You are on page 1of 13

OO Concepts:

Inheritance and Polymorphism


Atul Gupta

Inheritance








A relationship between a set of closely related classes


having similarities and differences
Superclass-subclass relationship
A subclass inherits all the members of its parent and
may add attributes and methods of its own
Superclass holds common attributes, operations, and
associations; subclasses add specific attributes,
operations, and associations
is-a or a-kind-of or a-type-of relationship
Depicts a hierarchy of relationship

Example - Inheritance
Figure

Diagram
name

ZeroDimensional

color
penType
move()
rotate()
display()

dimensionality

OneDimensional

TwoDimensional
fillColor

scale()

Point

Line

Arc

fill(); scale()

Spline

Polygon

Circle

x-coord
y-coord

startPoint
endPoint

radius
startAngle

controlPts

numOfSides
vertices

radius
centre

display()

display()

display()

display()

display()

display()
rotate()

Inheritance


A subclass inherits attributes and methods from its


superclass
Inheritance is also referred as GeneralizationSpecialization relationship opposite perspectives
Inheritance is also referred as Classification

Multiple Generalization

Use of Generalization





To structure the description of objects forming


taxonomies dealing with complexity
To enable reuse of code
To support polymorphism



Call an operation at the superclass level


Add new subclasses

Overriding Features





Provide more specificity to inherited information


Boost performance
Never override the signature, or form of a feature
Should be done with great care

Abstract Class








A class whose partial or complete implementation is


deferred to subclasses
A class that can not be instantiated
Normally, superclasses in a hierarchy, that dont make
sense for their instanciation, are declared as abstract
Abstract classes may contain abstract methods
Abstract methods are methods with no body
specification
A class containing at least one abstract method is to be
declared as abstract

Example - Inheritance
Figure

Diagram
name

ZeroDimensional

color
penType
move()
rotate()
display()

OneDimensional

TwoDimensional
fillColor
fill()

Point

Line

Arc

Spline

Polygon

Circle

x-coord
y-coord

startPoint
endPoint

radius
startAngle

controlPts

name
address

name
address

display()

display()

display()

display()

display()

display()
rotate()

Multiple - Inheritance
Vehicle

LandVehicle

Car

WaterVehicle

AmphibiousVehicle

Boat

Class Relationships: An Example

Polymorphism



Inheritance enables polymorphism


Three types




Overloaded Methods and Attributes


Overridden Methods
Dynamic Binding

In a sound hierarchy, a parent behavior can be


substituted with a child behavior [Liskov-88]

Summary


Inheritance is used





To structure the description of objects forming taxonomies


dealing with complexity
To enable reuse of code
To support polymorphism



Call an operation at the superclass level


Add new subclasses

Classes with incompletely specified behavior are


declared as Abstract

You might also like