BAB IX
Object Oriented
Programming
A programming oriented to object processing rather than data.
Nowadays, the programming focuses on object rather than just
a heap of data.
Many object-oriented programming (OOP) languages
· Some support
procedural and data-oriented programming (e.g., Ada 95+ and C++)
· Some support
functional program (e.g., CLOS)
· Newer
languages do not support other paradigms but use their imperative structures
(e.g., Java and C#)
· Some are
pure OOP language (e.g., Smalltalk & Ruby)
· Some
functional languages support OOP, but they are not discussed in this chapter
Object-Oriented Programming
· Three major
language features:
o Abstract data
types (Chapter 11)
o Inheritance
§ Inheritance is the
central theme in OOP and languages that support it
§ Productivity
increases can come from reuse
· ADTs are
difficult to reuse—always need changes
· All ADTs are
independent and at the same level
§ Inheritance allows
new classes defined in terms of existing
ones, i.e., by allowing them to inherit common parts
§ Inheritance
addresses both of the above concerns--reuse ADTs after minor changes and define
classes in a hierarchy
o Polymorphism
· Concepts
o ADTs are usually
called classes
o Class instances
are called objects
o A class that
inherits is a derived class or a subclass
o The class from
which another class inherits is a parent class or superclass
o Subprograms that
define operations on objects are called methods
o Calls to methods
are called messages
o The entire
collection of methods of an object is called its message protocol or message
interface
o Messages have two
parts--a method name and the destination object
o In the simplest
case, a class inherits all of the entities of its parent
o Inheritance can be
complicated by access controls to encapsulated entities
§ A class can hide
entities from its subclasses
§ A class can hide
entities from its clients
§ A class can also
hide entities for its clients while allowing its subclasses to see them
o Besides inheriting
methods as is, a class can modify an inherited method
§ The new one
overrides the inherited one
§ The method in the
parent is overridden
o Three ways a class
can differ from its parent:
1. The parent class can define some of its variables or
methods to have private access, which means they will not be visible in the
subclass
2. The subclass can add variables and/or methods to those
inherited from the parent
3. The subclass can modify the behavior of one or more of
its inherited methods.
o There are two
kinds of variables in a class:
§ Class variables -
one/class
§ Instance variables
- one/object
o There are two
kinds of methods in a class:
§ Class methods –
accept messages to the class
§ Instance methods –
accept messages to objects
o Single vs.
Multiple Inheritance
o One disadvantage
of inheritance for reuse:
§ Creates
interdependencies among classes that complicate maintenance
Dynamic Binding
· A
polymorphic variable can be defined in a class that is able to reference (or
point to) objects of the class and objects of any of its descendants
· When a class
hierarchy includes classes that override methods and such methods are called
through a polymorphic variable, the binding to the correct method will be
dynamic
· Allows
software systems to be more easily extended during both development and maintenance
· Dynamic
Binding Concepts
o An abstract method
is one that does not include a definition (it only defines a protocol)
o An abstract class
is one that includes at least one virtual method
o An abstract class
cannot be instantiated
Design Issues for OOP Languages
· The
Exclusivity of Objects
o Everything is an
object
§ Advantage -
elegance and purity
§ Disadvantage - slow
operations on simple objects
o Add objects to a
complete typing system
§ Advantage - fast
operations on simple objects
§ Disadvantage -
results in a confusing type system (two kinds of entities)
o Include an
imperative-style typing system for primitives but make everything else objects
§ Advantage - fast
operations on simple objects and a relatively small typing system
§ Disadvantage -
still some confusion because of the two type systems
· Are
Subclasses Subtypes?
o Does an “is-a”
relationship hold between a parent class object and an object of the subclass?
§ If a derived class
is-a parent class, then objects of the derived class must behave the same as
the parent class object
o A derived class is
a subtype if it has an is-a relationship with its parent class
§ Subclass can only
add variables and methods and override inherited methods in “compatible” ways
· Single and
Multiple Inheritance
o Multiple
inheritance allows a new class to inherit from two or more classes
o Disadvantages of
multiple inheritance:
§ Language and
implementation complexity (in part due to name collisions)
§ Potential
inefficiency - dynamic binding costs more with multiple inheritance (but not
much)
o Advantage:
§ Sometimes it is
quite convenient and valuable
· Object
Allocation and Deallocation
o From where are
objects allocated?
§ If they behave line
the ADTs, they can be allocated from anywhere
· Allocated
from the run-time stack
· Explicitly
create on the heap (via new)
§ If they are all
heap-dynamic, references can be uniform thru a pointer or reference variable
· Simplifies
assignment - dereferencing can be implicit
§ If objects are
stack dynamic, there is a problem with regard to subtypes – object slicing
o Is deallocation
explicit or implicit?
· Dynamic and
Static Binding
o Should all binding
of messages to methods be dynamic?
§ If none are, you
lose the advantages of dynamic binding
§ If all are, it is
inefficient
o Maybe the design
should allow the user to specify
· Nested
Classes
o If a new class is
needed by only one class, there is no reason to define so it can be seen by
other classes
§ Can the new class
be nested inside the class that uses it?
§ In some cases, the
new class is nested inside a subprogram
rather than directly in another class
o Other issues:
§ Which facilities of
the nesting class should be visible to the nested class and vice versa
·
Initialization of Objects
o Are objects
initialized to values when they are created?
§ Implicit or
explicit initialization
o How are parent
class members initialized when a subclass object is created?
Support for OOP in C++
· General
Characteristics:
o Evolved from C and
SIMULA 67
o Among the most
widely used OOP languages
o Mixed typing system
o Constructors and
destructors
o Elaborate access
controls to class entities
· Inheritance
o A class need not
be the subclass of any class
o Access controls
for members are
o Private (visible
only in the class and friends) (disallows subclasses from being subtypes)
o Public (visible in
subclasses and clients)
o Protected (visible
in the class and in subclasses, but not clients)
· Example
class base_class {
private:
int a;
float x;
protected:
int b;
float y;
public:
int c;
float z;
};
class subclass_1 : public base_class { … };
// In this one, b
and y are protected and
// c and z are
public
class subclass_2 : private base_class { … };
// In this one, b,
y, c, and z are private,
// and no derived
class has access to any
// member of
base_class
·
Reexportation in C++
o A member that is
not accessible in a subclass (because of private derivation) can be declared to
be visible there using the scope resolution operator (::), e.g.,
class subclass_3 : private base_class {
base_class ::
c;
…
}
· Multiple
inheritance is supported
o If there are two
inherited members with the same name, they can both be referenced using the
scope resolution operator (::)
class Thread { ... }
class Drawing { ... }
class DrawThread : public Thread, public Drawing { … }
· Dynamic
Binding
o A method can be
defined to be virtual, which means that they can be called through polymorphic
variables and dynamically bound to messages
o A pure virtual
function has no definition at all
o A class that has
at least one pure virtual function is an abstract class
· Example
class Shape {
public:
virtual void
draw() = 0;
...
};
class Circle :
public Shape {
public:
void draw() {
... }
...
};
class Rectangle :
public Shape {
public:
void draw() {
... }
...
};
class Square :
public Rectangle {
public:
void draw() {
... }
...
};
Square* sq = new Square;
Rectangle* rect = new Rectangle;
Shape* ptr_shape;
ptr_shape = sq; //
points to a Square
ptr_shape ->draw();
// Dynamically
// bound
to draw in Square
rect->draw(); //
Statically bound to
// draw
in Rectangle
· If objects
are allocated from the stack, it is quite different
Square sq; // Allocates a Square object from the stack
Rectangle rect; // Allocates a Rectangle object from the
stack
rect = sq; // Copies the data member values from sq
object
rect.draw(); // Calls the draw from Rectangle
Implementing OO Constructs
· Two
interesting and challenging parts
o Storage structures
for instance variables
o Dynamic binding of
messages to methods
· Instance
Data Storage
o Class instance
records (CIRs) store the state of an object
§ Static (built at
compile time)
o If a class has a
parent, the subclass instance variables are added to the parent CIR
o Because CIR is
static, access to all instance variables is done as it is in records
§ Efficient
· Dynamic
Binding of method calls
o Methods in a class
that are statically bound need not be involved in the CIR; methods that will be
dynamically bound must have entries in the CIR
§ Calls to
dynamically bound methods can be connected to the corresponding code thru a
pointer in the CIR
§ The storage
structure is sometimes called virtual method tables (vtable)
§ Method calls can be
represented as offsets from the beginning of the vtable
Tidak ada komentar:
Posting Komentar