2. 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
4. 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
5. 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
7. 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;
};
8. 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
10. 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
11. 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
12. 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.
13. 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
19. 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.
22. 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();
};
23. 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.
24. 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); }