SlideShare a Scribd company logo
Inheritance and polymorphism
Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .
Ltd
Inheritance and
Polymorphism
Inheritance
• Derive a new class (subclass) from an existing
class (base class or superclass).
• Declaring subclasses
class B extends A
{ . . . }
– means class B is a specialization of class A
– the "is a" relationship exists
– a B object is an A object
A
B
"is a"
increasingly
general
increasingly
specialized
Inheritance
• Other names:
– superclass also called "parent class"
– subclass also called "child class"
• These names help understand concept of
inheritance
• Child class inherits characteristics of parent
class
– attributes
– methods
C++ Example
class A
{
public:
integer d;
};
class B : public A
{
public:
};
C++ Example
• The class B in the example does not have any direct data
member does it?
• Yes, it does. It inherits the data member d from class A.
• When one class inherits from another, it
acquires all of its methods and data.
• We can then instantiate an object of class B and call
into that data member.
void func()
{
B b;
b.d = 10;
};
Example: A Trip to Aviary
• Consider a collection of birds
which have different properties
– name
– color (some of the same name
are of different colors)
– they eat different things
– they make different noises
– some make multiple kinds of
sounds
Heirarchy Bird
call: ?
color:?
food:?
movement:?
WalkingBird
call: ?
color:?
food:?
movement:walked
FlyingBird
call: ?
color:?
food:?
movement:flew
Goose
call: honk
color: gray
food: bugs
Ostrich
call: neek-neek
color: brown
food: grass
Parrot
call: Squawk
color:?
food: fruit
Owl
call:?
color:?
food:mice
TalkingParrot
. . .
Bird class
• Note Bird class is a super class, previous fig
• All subclasses are derived from this bird class.
• Attributes common to all birds
– color
– food
– movement
Inheritance
• When we say …
class TalkingParrot extends Parrot
{ … }
– then a TalkingParrot object inherits all
Parrot attributes
– (which, in turn, inherits both FlyingBird
and Bird attributes)
• In general, descendant classes inherit the
attributes of ancestor classes
Results of Inheritance
• Used to eliminate redundant coding
• When we send toString() message to a
Goose or Parrot or TalkingParrot
object
– none of these classes implement the
toString() method
– but … they inherit it from Bird
–toString() need not be redefined in the
subclasses.
Don’t
• Consider the declaration:
Bird abird = new Goose();
– this is legal
– a Goose object "is a" Bird object
• Contrast
Goose aGoose = new Bird("gray",
"walking", "bugs");
– this is NOT legal
– A Bird object is not necessarily a Goose object
Types of Inheritance
• Multiple Inheritance
Multiple inheritance
Multilevel Inheritance
Hybrid Inheritance
Polymorphism
Polymorphism
• polymorphism (from the Greek meaning
"having multiple forms") is the characteristic
of being able to assign a different meaning to
a particular symbol or "operator" in different
contexts.
• Polymorphism is about an objects ability to
provide context when methods or operators
are called on the object.
Example
• class A
{
public:
virtual void f()=0;
};
• class B
{
public:
virtual void f()
{std::cout << "Hello from B" << std::endl;};
};
Example
• class C
{
public:
virtual void f()
{std::cout << "Hello from C" << std::endl;};
};
Example
• If I have an object A, then calling the method
f() will produce different results depending on
the context, the real type of the object A.
• func(A & a)
{
A.f();
};
Overloading
• Ability of one function to perform different
tasks.
• Creating several methods with the same
name which differ from each other in the type
of the input and the output of the function.
• The overloaded function must differ by data
types.
Example
• main()
• { cout<<volume(10); cout<<volume(2.5,8);
cout<<volume(100,75,15); }
• // volume of a cube
int volume(int s)
{ return(s*s*s); }
• // volume of a cylinder
double volume(double r,int h)
{ return(3.14*r*r*h); }
• // volume of a cuboid
long volume(long l,int b,int h)
{ return(l*b*h); }
Overriding
•  overriding is a concept used in inheritance 
which involves a base class implementation of 
a method .
•  Then in a subclass, you would make another 
implementation of the method. 
• Here is one simple example
Example
• class Base
{
public:
virtual void DoSomething() {x = x + 5;}
private:
int x;
};
class Derived : public Base
{
public:
virtual void DoSomething() { y = y + 5; Base::DoSomething(); }
private:
int y;
}; 
Any Questions ?
Inheritance and polymorphism
If this presentation helped you, please visit our page 
facebook.com/baabtra and like it. 
Thanks in advance. 
 
www.baabtra.com | www.massbaab.com |www.baabte.com
Contact Us

More Related Content

PPSX
Modules and packages in python
PPTX
Inheritance in c++
PPTX
Encapsulation C++
PPT
Functions in C++
PPTX
Constructor in java
PPT
RECURSION IN C
PDF
Constructor and Destructor
Modules and packages in python
Inheritance in c++
Encapsulation C++
Functions in C++
Constructor in java
RECURSION IN C
Constructor and Destructor

What's hot (20)

PPTX
Oops concept in c++ unit 3 -topic 4
PPTX
Object oriented programming
PPSX
Data types, Variables, Expressions & Arithmetic Operators in java
PPTX
07. Virtual Functions
PDF
06. operator overloading
PPTX
concept of oops
PPTX
Polymorphism in c++(ppt)
PPT
Memory allocation in c
PPTX
C++ presentation
PPTX
C++ Object Oriented Programming
PPTX
Templates in C++
PPTX
Constructors in C++
PPTX
Polymorphism In c++
PPTX
Intro to c++
PPTX
PPTX
Recursive Function
PDF
Python functions
PDF
Object oriented approach in python programming
PPTX
Object oriented programming in python
PDF
Constructors and destructors
Oops concept in c++ unit 3 -topic 4
Object oriented programming
Data types, Variables, Expressions & Arithmetic Operators in java
07. Virtual Functions
06. operator overloading
concept of oops
Polymorphism in c++(ppt)
Memory allocation in c
C++ presentation
C++ Object Oriented Programming
Templates in C++
Constructors in C++
Polymorphism In c++
Intro to c++
Recursive Function
Python functions
Object oriented approach in python programming
Object oriented programming in python
Constructors and destructors
Ad

Viewers also liked (20)

PPT
C++ polymorphism
PDF
Mari Memahami PSR (PHP Standards Recommendation)
PDF
Restful api design
PPT
Introduction to OOP with PHP
PDF
Laravel 5 Tutorial : Membuat Blog Sederhana dengan Laravel 5.3
PDF
Nge-GIT (Belajar Git Bareng)
PDF
Tutorial dasar laravel
ODP
OOP Basic - PHP
DOCX
Fungsi-Fungsi PHP
PDF
객체지향 개념 (쫌 아는체 하기)
PDF
LinkedIn SlideShare: Knowledge, Well-Presented
PDF
What Makes Great Infographics
PDF
Masters of SlideShare
PDF
STOP! VIEW THIS! 10-Step Checklist When Uploading to Slideshare
PDF
You Suck At PowerPoint!
PDF
10 Ways to Win at SlideShare SEO & Presentation Optimization
PDF
How To Get More From SlideShare - Super-Simple Tips For Content Marketing
PDF
A Guide to SlideShare Analytics - Excerpts from Hubspot's Step by Step Guide ...
PDF
2015 Upload Campaigns Calendar - SlideShare
PPTX
What to Upload to SlideShare
C++ polymorphism
Mari Memahami PSR (PHP Standards Recommendation)
Restful api design
Introduction to OOP with PHP
Laravel 5 Tutorial : Membuat Blog Sederhana dengan Laravel 5.3
Nge-GIT (Belajar Git Bareng)
Tutorial dasar laravel
OOP Basic - PHP
Fungsi-Fungsi PHP
객체지향 개념 (쫌 아는체 하기)
LinkedIn SlideShare: Knowledge, Well-Presented
What Makes Great Infographics
Masters of SlideShare
STOP! VIEW THIS! 10-Step Checklist When Uploading to Slideshare
You Suck At PowerPoint!
10 Ways to Win at SlideShare SEO & Presentation Optimization
How To Get More From SlideShare - Super-Simple Tips For Content Marketing
A Guide to SlideShare Analytics - Excerpts from Hubspot's Step by Step Guide ...
2015 Upload Campaigns Calendar - SlideShare
What to Upload to SlideShare
Ad

Similar to Inheritance and polymorphism (20)

PPTX
Lecture 3
PPT
chap11.ppt
PDF
Java programming -Object-Oriented Thinking- Inheritance
PPT
Introductory Example: A Trip to the Aviary.ppt
PPSX
Inheritance and Polymorphism in Oops
PPTX
Introduction to inheritance and different types of inheritance
PDF
‏‏‏‏‏‏oop lecture objectives will come.pdf
PDF
Object oriented programming inheritance
PPTX
Object Oriented Design and Programming Unit-03
PPT
10.Inheritance.ppt for oops programinggg
PDF
OOP Inheritance
PPTX
inheritance in OOPM
PPTX
METHOD OVERLOADING AND INHERITANCE INTERFACE
PPT
Unit 3 Java
PDF
Inheritance and Substitution
PPTX
INHERITANCES.pptx
PPTX
Ritik (inheritance.cpp)
PPTX
Inheritance in oops
PPTX
Aryan's pres. entation.pptx
PPTX
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
Lecture 3
chap11.ppt
Java programming -Object-Oriented Thinking- Inheritance
Introductory Example: A Trip to the Aviary.ppt
Inheritance and Polymorphism in Oops
Introduction to inheritance and different types of inheritance
‏‏‏‏‏‏oop lecture objectives will come.pdf
Object oriented programming inheritance
Object Oriented Design and Programming Unit-03
10.Inheritance.ppt for oops programinggg
OOP Inheritance
inheritance in OOPM
METHOD OVERLOADING AND INHERITANCE INTERFACE
Unit 3 Java
Inheritance and Substitution
INHERITANCES.pptx
Ritik (inheritance.cpp)
Inheritance in oops
Aryan's pres. entation.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx

More from baabtra.com - No. 1 supplier of quality freshers (20)

PPTX
Agile methodology and scrum development
PDF
Acquiring new skills what you should know
PDF
Baabtra.com programming at school
PDF
99LMS for Enterprises - LMS that you will love
PPTX
Chapter 6 database normalisation
PPTX
Chapter 5 transactions and dcl statements
PPTX
Chapter 4 functions, views, indexing
PPTX
PPTX
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
PPTX
Chapter 1 introduction to sql server
PPTX
Chapter 1 introduction to sql server
Agile methodology and scrum development
Acquiring new skills what you should know
Baabtra.com programming at school
99LMS for Enterprises - LMS that you will love
Chapter 6 database normalisation
Chapter 5 transactions and dcl statements
Chapter 4 functions, views, indexing
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 1 introduction to sql server
Chapter 1 introduction to sql server

Recently uploaded (20)

PDF
RMMM.pdf make it easy to upload and study
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Business Ethics Teaching Materials for college
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Insiders guide to clinical Medicine.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Basic Mud Logging Guide for educational purpose
RMMM.pdf make it easy to upload and study
human mycosis Human fungal infections are called human mycosis..pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
Business Ethics Teaching Materials for college
Supply Chain Operations Speaking Notes -ICLT Program
VCE English Exam - Section C Student Revision Booklet
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Week 4 Term 3 Study Techniques revisited.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Final Presentation General Medicine 03-08-2024.pptx
Insiders guide to clinical Medicine.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Module 4: Burden of Disease Tutorial Slides S2 2025
Anesthesia in Laparoscopic Surgery in India
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Microbial disease of the cardiovascular and lymphatic systems
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Abdominal Access Techniques with Prof. Dr. R K Mishra
Basic Mud Logging Guide for educational purpose

Inheritance and polymorphism