Terminology of inheritance

Download Report

Transcript Terminology of inheritance

Lecture-8
Inheritance & Classification
Hierarchies
Inheritance & Classification
Hierarchies


Inheritance is one of the most powerful features of O-O
programming
By organising classes into Classification Hierarchies we
can
add extra dimensions to encapsulation by grouping
ADT's
 and enables classes to inherit from other classes
 thus extending the attributes and methods of the class
which inherits


The inheriting class may then also add extra functionality
and attributes to produce a new object
Terminology of inheritance





The following terms are used to describe the different
inheritance properties of an object
Derived Class or Sub Class or Child Class
 A Class which inherits some of its attributes and
methods from another class
Base Class or superclass or Parent Class
 A Class from which another class inherits
Ancestor
 A Classes ancestors are those from which its own
superclass inherit
Descendant
 A classes descendants are those which inherit from its
superclasses
Why do we need Inheritance ?


What is the purpose of inheritance and why should
we wish to inherit the attributes and methods of
other Objects ?
There are two main reasons for using inheritance
in O-O which are

Specialisation


Extending the functionality of an existing class
Generalisation

Sharing commonality between two or more classes
Classification Hierarchy




The product of inheritance is a classification Hierarchy
This is a relationship between classes where one class is
said to be 'a kind of' other class
As the class hierarchy is traversed from top to bottom we
move from generalisation to specialisation of classes
This is done by adding functionality to extend what exists
at each level of the class hierarchy from the base class
An Example
Geometric Object
2D
Triangle
3D
Square
Sphere
Cube
Class Hierarchy Diagram



The base of this diagram is the Geometric Object class
From this the 2D and 3D objects inherit the base features (
what could these be ?)
After this there are specialised versions of the classes using
the 'kind of' relationship
where 2D objects such as triangle and Square inherit
the base classes of a Geometric Object and then add
special features specific to the type of object
The same then applies for the other classes



This tree runs from a generalisation of the base class to a
specialisation of the actual object
'a kind of' or 'a part of'





Each level of a classification hierarchy contains more specific
types of class
each one of which must be 'a kind of' the class from which it
inherits
This distinction is important as the difference between 'a kind of'
and 'a part of' is very different
The distinction between different Objects and the same Objects
with different states
This mean analysis to see if difference of Objects are
dependent upon the Object type or a state of that Object
 For Example Equilateral Triangle and Isosceles Triangle are
inappropriate Object classifications as they are both really
triangles (with just the angle between vertices being
different) which we could store in a general triangle class
What Do Objects Inherit ?





A class does not contain any state values, only a 'blue
print' for what value are to be contained
Therefore a 'derived class' inherits all of the attributes from
the 'base class'
A derived class is by definition identical to the base class,
but it can be built on to extend and modify the base class
Objects of the derived class do not inherit anything from
the objects of the base class
As far as objects are concerned there is no hierarchy
Example
Line
+startPosition:
+endPosition:
+Draw()
ColoredLine
+color
+set color()
Line
Object
start = (x,y)
end = (x,y)
Line
Object
start = (x,y)
end = (x,y)
color=?
draw(star,end)
draw(star,end)
set color(col)