|
Section 6 :
Overloading, Overriding, Runtime Type, and Object
Orientation
State the benefits of
encapsulation in object oriented design and write code
that implements tightly encapsulated classes and the
relationships "is a" and "has a".
Benefits of Object oriented
implementation
These benefits accrue
from two particular features. The first of these and
perhaps the most important is the notion of abstract
data type. The second of these is the extensibility
provided by inheritance.
Abstract data type
An abstract data type
simply means a well encapsulated aggregate of data and
behavior. The primitive data types defined in any
programming language are in fact abstract data types,
albeit not user defined. The state of an object should
be represented only with variables of
private accessibility.
All behavior should be accessed only via methods. By
insisting that the variables inside an object are not
accessible outside the object, you ensure that the
nature of those variables is irrelevant outside the
object. This in turn means that you can change the
nature of the storage, for maintenance purposes, for
performance improvement, or for any other reason
freely. This freedom is one of the greatest benefits
of encapsulation.
Inheritance:
For this please see the next section on overloading
and overriding.
"is a" and "has a"
An object can perform
many functions and may contain different types of
data. The source of these data and behavior may come
from two sources. The first source is from inheritance
from its parent classes and the other source is any
object it may contain as its state.
Each class in Java,
except Object class, is a subclass of another. In any
class hierarchy, any class can be called by the name
of any class which is above it in the hierarchy. Let
us consider a class called Vehicle. One of its
subclasses can be called Car. Cars can of different
models like Ford or Mercedes. Now any Ford can be
called a Car. It is also a vehicle. This relationship
is known as "is a" relationship.
A car has many
components, each of which can belong to a different
hierarchy of classes. For example, let us consider
brakes. Brakes can be of different types (classes).
There can be a whole hierarchy of brakes. Since a car
has brakes, this relationship is known as "has a"
relationship.
section6-1 | section6-2 | section6-3
Sections :
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11
If you wish to download the complete notes
(around 100 pages - pdf or doc file) for a small price, click here.
|