OO technology is based on several concepts that, when applied to a
program, make the resulting product easier to extend and reuse.
Five concepts: Abstraction, encapsulation, specialization, type
- Abstraction - process of focusing only on the essential
characteristics of an entity
- Abstraction - collection of attributes and behaviors that
define a model appropriate from some specified purpose/application
Ex: Sales Person - abstractions for 2 different applications (in class)
An abstraction is represented by an interface specification which
specifies the behavior of the module.
A class defines an abstraction in the problem domain.
- The implementation of the interface of a module should
not be visible outside the module.
- Encapsulation = "information hiding" or "implementation
- Java enables encapsulation through visibility attributes of
instance variables and methods.
Ex: Defining properties of a class as private to the class is an
example of encapsulation.
- Encapsulation allows multiple implementations of an
interface. If the implementation is invisible to the client, the
implementation can be changed without affecting the client at all.
- Object-oriented languages support class specialization, the
is-a-kind-of relationship, between classes.
1. The Kangaroo class is a specialization
of the Animal class (i.e. Kangaroo is a kind of Animal).
2. The CashTextBox class is a specialization of the TextBox class (i.e.
the TextBox class is a generalization
of the CashTextBox class).
Inheritance: the specialization inherits all of the state and behavior
of the generalization
- Generalization - process of identifying common properties
among objects (either real-world objects or their abstractions).
- Specialization - adding attributes and behaviors to a core
set of attributes and behaviors.
Examples (in class)
The concepts of abstraction and generalization are often confused:
Abstraction - simplify the description of an entity.
Generalization - look for common properties among the
Specialization lets you define properties common to all animals in the
Animal class - and then inherit these properties in the Animal
specializations (Kangaroo, Dog, Cat, Lizard, etc). So you only have a
single copy of these properties. If you had multiple copies of this
code, corrections to the code would be made to each copy.
Note: Specialization is a common way of evolving existing object
structures. But specialization should be used with care.
That is: Exercise Caution
when creating Inheritance Hierarchies
Killer Kangas - An "Object" Lesson
Project: Australian air force - virtual reality
simulator for helicopter combat training
- Programmers modelled the movement of kangaroos and their
reaction to helicopters.
- Re-used code used to model infantry detachment reactions
under same stimuli - changed icon from soldier to kangaroo, increased
speed of movment for figures.
- Result of kangaroo inheriting from soldier: kangaroos
scattered in reaction to helicopter (expected). But also an unexpected
- Treat a more specific type of object as a more general type.
E.g. refer to a Kangaroo instance either as a Kangaroo or a more
general Animal. This idea is called substitution.
When the client expects a reference to an Animal, you can substitute
any more specific reference (a reference to an Kangaroo or other Animal
- Type promotion lets you write code that only refers to more
general types, like Animal. So when you define a new Animal
specialization, your client code is unaffected - the instances of the
new class are treated by the client code as generic Animals.
- Polymorphism is the complement of type promotion.
- Polymorphism - objects act according to their actual type,
regardless of how they are referenced.
- Type promotion and polymorphism let you write code that is
not dependent on particular specializations. Your client code refers
only to generic Animals, and so the addition or removal of Animal
specializations does not affect the client code. But when the client
code acts on an Animal, you get the correct behavior based on the
actual type (Dog, Kangaroo, etc) of the object at run time.