Classes & Objects

advertisement
Classes & Objects
Computer Science I
Last updated 9/30/10
Object-Oriented Programming


OOP: A programming paradigm that uses "objects" –
data structures consisting of data fields and methods
together with their interactions – to design
applications and computer programs
Programming techniques may include …





Data abstraction
Encapsulation
Modularity
Polymorphism
Inheritance
Background



Object-oriented programming has roots that can be
traced to the 1960s
As hardware and software became increasingly
complex, manageability often became a concern
Researchers studied ways to maintain software
quality and developed object-oriented programming
in part to address common problems by strongly
emphasizing discrete, reusable units of programming
logic
Background





OOP focuses on data rather than processes
Programs are composed of self-sufficient modules
("classes")
Each instance of which ("objects") contains all the
information needed to manipulate its own data
structure ("members")
Modular programming focuses on the function of a
module, rather than specifically the data
MP provides for code reuse, and self-sufficient
reusable units of programming logic, enabling
collaboration through the use of linked modules
(subroutines)
Background


An object-oriented program may be viewed as a
collection of interacting objects
Each object is capable of …



receiving messages,
processing data, and
sending messages to other objects
Background



Each object can be viewed as an independent
'machine' with a distinct role or responsibility
Actions (methods) on these objects are closely
associated with the object
For example …



OOP data structures tend to carry their own operators
around with them
Or at least inherit them from a similar object or class
In the conventional model, the data and operations
on the data don't have a tight, formal association
What is a class?




A template for an object
A user-defined datatype that contains the variables,
properties and methods in it
Defines the abstract characteristics of a thing
(object), including its characteristics and the thing's
behaviors
For example …

The class Dog would consist of traits shared by all dogs,
such as breed and fur color (characteristics), and the ability
to bark and sit (behaviors)
What is a class?




Provides modularity and structure in an objectoriented computer program
Should typically be recognizable to a nonprogrammer familiar with the problem domain,
meaning that the characteristics of the class should
make sense in context
The code should be relatively self-contained
Collectively, the defined properties and methods are
called members
What is a class?


One can have an instance of a class; the instance is
the actual object created at run-time
For example …



The Lassie object is an instance of the Dog class
The set of attribute values for a particular object is
called its state
The object consists of state and the behavior that's
defined in the object's classes
What is an object?




A discrete bundle of functions and procedures, often
relating to a particular real-world concept such as a
bank account holder or hockey player
Other pieces of software can access the object only
by calling its functions and procedures that have
been allowed to be called by outsiders
Some agree that isolating objects in this way makes
their software easier to manage and keep track of
Others feel that software becomes more complex to
maintain and document, or even to engineer from
the start
OOP Features





Dynamic
Encapsulation
(or multi-methods, in which case the state is kept
separate)
Polymorphism
Inheritance
(or delegation)
Open recursion
What is a method?

"The process by which an object sends data to
another object or asks the other object to invoke a
method.“ *

Also known as interfacing

For example…


The object called Breeder may tell the Lassie object to sit by
passing a sit message that invokes Lassie's sit() method
The syntax varies between languages
* Armstrong, The Quarks of Object-Oriented Development. In descending order of popularity, the "quarks" are:
Inheritance, Object, Class, Encapsulation, Method, Message Passing, Polymorphism, Abstraction
Dynamic Dispatch



When a method is invoked on an object, the object
itself determines what code gets executed by looking
up the method at run time in a table associated with
the object
This feature distinguishes an object from an abstract
data type (or module), which has a fixed (static)
implementation of the operations for all instances
It is a programming methodology that gives modular
component development while at the same time
being very efficient
Encapsulation


Conceals the functional details of a class from objects
that send messages to it
For example …





The Dog class has a bark() method variable
The code for the bark() method defines exactly how a bark
happens
Timmy, Lassie's friend, however, does not need to know
exactly how she barks
Encapsulation is achieved by specifying which classes may
use the members of an object
The result is that each object exposes to any class a certain
interface — those members accessible to that class
Encapsulation

Rationale:



Prevents clients of an interface from depending on those
parts of the implementation that are likely to change in the
future
Thereby allowing those changes to be made more easily
without changes to clients
For example…


An interface can ensure that puppies can only be added to an object of
the class Dog by code in that class
Members are often specified as public, protected or private,
determining whether they are available to all classes, subclasses or only the defining class
Polymorphism




Allows the programmer to treat derived class
members just like their parent class's members
More precisely, the ability of objects belonging to
different data types to respond to calls of methods of
the same name, each one according to an
appropriate type-specific behavior
One method, or an operator such as +, -, or *, can
be abstractly applied in many different situations
For example …


If a Dog is commanded to speak(), this may elicit a bark()
If a Pig is commanded to speak(), this may elicit an oink()
Inheritance



A process in which a class inherits all the state and
behavior of another class
This is called child-parent or is-a relationship
Subclasses are more specialized versions of a class,
which inherit attributes and behaviors from their
parent classes, and can introduce their own
Inheritance

For example …




The class Dog might have sub-classes called Collie,
Chihuahua, and GoldenRetriever
Lassie would be an instance (object) of the Collie subclass
Assume that the Dog class defines a method called bark()
and a property called furColor
Each of the sub-classes will inherit these members; the
programmer only needs to write the code for them once
Inheritance

Each subclass can alter its inherited traits

For example …


The Collie subclass might specify that the default furColor
for a collie is brown-and-white
The Chihuahua subclass might specify that the bark()
method produces a high pitch by default
Inheritance

Subclasses can also add new members

For example …



The Chihuahua subclass could add a method called
tremble()
An individual Chihuahua instance would then use a highpitched bark() from the Chihuahua subclass, which in turn
inherited the usual bark() from Dog
The Chihuahua object would also have the tremble()
method, but Lassie would not, because she is a Collie, not a
Chihuahua
Inheritance


Inheritance is an "a… is a" relationship between
classes, while instantiation is an "is a" relationship
between an object and a class
For example …



a Collie is a Dog ("a… is a"), but
Lassie is a Collie ("is a")
Thus, the object named Lassie has the methods from both
classes Collie and Dog
Open recursion


A special variable (syntactically it may be a keyword),
usually called this or self, that allows a method body
to invoke another method body of the same object
This variable is late-bound ; it allows a method
defined in one class to invoke another method that is
defined later, in some subclass thereof
Download