Object-Oriented Programming
Principles of OOP
Abstraction
- Implementation hiding
- Providing a minimal interface that offers
all the
behavior and attributes the intended user expects, without revealing
the
internal implementation details
- Allows the implementation to vary without
changes having
to be made to the external usage of the class.
- Allows the external usage of the class to
vary without
changes having to be made to the implementation of the class.
- A design that violates abstraction is
said to be
intrusive.
Encapsulation
- Combining data and the functions that act
on it, within a
class.
- Data is maintained by the class.
- Data hiding
- Data within the class is hidden from the
rest of the
program and is only accessible through methods of the class via the
public interface - getters / setters; inspectors / mutators.
- Information hiding; implementation
details are kept
hidden .
Inheritance
- Heirarchical reuse of behavior and
attributes between
similar classes.
- Deriving specialized versions of general
classes.
- Overriding some of the behavior of a
class.
Weaknesses of inheritance
- Inheritance violates abstraction.
- Code reuse (via inheritance) is
intrusive.
- Polymorphism (via inheritance) is
intrusive.
- Using inheritance for code reuse
requires knowledge of
the implementation details of the parent class; the child class cannot
be designed and maintained independently of the parent class.
- Polymorphism is only possible for the
child class if
the parent class supports it; the parent class cannot be designed and
maintained independently of the child class.
- Multiple inheritance is especially
problematic due to
occasional naming overlaps between the methods or attributes of the
parent classes.
Prefer delegation to inheritance
- Delegation supports abstraction.
- Code reuse (via delegation) is
non-instrusive.
- Polymorphism (via delegated
interfaces) is
non-intrusive.
- Inheritance is falling out of favor,
much as goto
statements did in earlier decades.
- An alternative to inheritance is for
the child class to
have an instance of the parent class as a data member and to access the
behavior and attributes of the parent class through the data member
(via
the public interface of the parent class).
- This changes the relationship between
the child class
and the parent class from "is-a" to "has-a", from generalization to
composition.
- The design approach for this,
delegation, helps make
composition as powerful for code reuse as inheritance.
- Delegation is more flexible; you can
delegate to a
different object at run-time, whereas the inheritance of a class is
fixed at compile-time.
- Interfaces, combined with delegation,
make it easier to
avoid inheritance.
- The child class implements the public
interface of the
parent class.
- The relevant method calls are delegated
to the parent
object.
- In this way, the instance of the child
class can be
used as if it were an instance of the parent class (which is one of the
benefits of inheritance).
- Multiple interfaces can be used in
place of multiple
inheritance.
- Polymorphism is more natural with
interface delegation
than with inheritance; rather than instrusively declaring virtual
methods in the parent class, as with inheritance, the child class
independently performs whatever actions needed in addition to or
instead
of delegating to the parent object.
Polymorphism
- (via inheritance) An object decides what
method to apply
to itself at run-time depending on its place in the inheritance
hierarchy.
- (via delegation) One or more objects are
accessed using
the same public interface. The objects are of different classes
or change to different classes over time, causing them to respond
differently to the same actions.
Data Members
- The attributes (data) of a class
- aka member variables, object variables,
class variables
Methods
- The behavior (functions) of a class.
- aka member functions, object functions,
class functions
Constructors
- Initializes an object when it's created.
Inspectors
- Returns the value of an attribute (data)
of the class.
- aka getters / accessor methods
Examples
- GetTime( )
- GetTemperature( )
Mutators
- Changes the value of an attribute (data)
of the class.
- aka setters
Examples
- SetTime( SomeTime )
- SetTemperature( SomeTemperature )
Facilitators
- Causes the object to perform an action or
service.