0% found this document useful (0 votes)
56 views13 pages

OOConcepts Inheritance Polymorphism

Inheritance allows classes to share attributes and behaviors through a hierarchical relationship. Subclasses inherit from superclasses and can add their own attributes and methods. This enables code reuse and polymorphism. Abstract classes defer full implementation to subclasses and cannot be instantiated directly. Inheritance supports polymorphism through method overriding and dynamic binding, allowing parent behaviors to be substituted by child behaviors.

Uploaded by

RaJu SinGh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views13 pages

OOConcepts Inheritance Polymorphism

Inheritance allows classes to share attributes and behaviors through a hierarchical relationship. Subclasses inherit from superclasses and can add their own attributes and methods. This enables code reuse and polymorphism. Abstract classes defer full implementation to subclasses and cannot be instantiated directly. Inheritance supports polymorphism through method overriding and dynamic binding, allowing parent behaviors to be substituted by child behaviors.

Uploaded by

RaJu SinGh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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