Lecture
No.05
Multiple
Inheritance
Inheritance:
We
saw inheritance purposes in last lecture
·
Generalization
·
Extention
or sub typing
·
Specialization
or restriction
Abstract
and concrete classes, former is used to represent abstract concepts later is
used to represent concrete concepts.
Overriding
derived classes override inherited classes (base classes) behaviour.
Overriding
is used for Specialization, Extention, Restriction, and Performance.
01.1. Multiple
Inheritance
Sometimes
we want to reuse characteristics of more than one parent class, in that case we
need to inherit a class from more than one classes.
Consider
the example of an imaginary specie Mermaid used in fairy tales that lives in
water having features both of a women as well as of a fish, In Object Oriented
programming perspective Mermaid can be derived from two classes Women and Fish.
C++ Code:
/*Program to demonstrate simple multiple inheritance*/
class Fish {
};
class Woman {
};
class Mermaid : public Woman , public
Fish {
};
|
Our Mermaid class inherits features of both woman
and fish suppose our woman class has method wald() and fish cclass has method
swim then our mermaid class can use both methods i.e can walk as well as can
c++
code:
#include <iostream>
#include <stdlib.h>
using namespace std;
/*Program to demonstrate simple multiple inheritance*/
class Fish
{
public:
void swim(){
cout<<"\n In method
swim";
}
};
class Woman
{
public:
void walk(){
cout<<"\n In method
walk"<<endl;
}
};
class Mermaid : public Woman,public
Fish
{
};
int main(int
argc, char *argv[])
{
Mermaid mermaid;
/*This Mermaid object will have two implicit objects one of Fish class
and one of
Woman class*/
mermaid.swim();
mermaid.walk();
system("PAUSE");
return 0;
}
|
Output:
In method[1] swim
In method walk
|
Example
2– Multiple Inheritance
Take another example of amphibious
vehicle (vehicle that can run on land as well as on water) so it has properties
of both land as well as of water vehicle. The general hierarchy in this case
will be,
Here we have added a general Vehicle
class as well to add all common functions of Land Vehicles and Water Vehicles
in that class, and specific functions of Land and Water vehicle in their
respective classes then we have derived Amphibious Vehicle class from Land
Vehicle and Water Vehicle classes (we can do the same in first example as well
concerning Woman, Fish and Mermaid).
C++ code:
class Vehicle
{
};
class WaterVehicle : public
Vehicle
{
};
class LandVehicle : public
Vehicle
{
};
class AmphibiousVehicle : public
LandVehicle,public
WaterVehicle
{
};
|
Suppose we have a changeGear
method in Vehicle class that is applicable to both water and land vehicle, we
also have Float and Move methods in water and land vehicles respectively then
our amphibious vehicle will have all these methods,
C++ code:
#include <iostream>
#include <stdlib.h>
using namespace std;
/*Multiple Inheritance in case of Amphibious Vehicle*/
class Vehicle
{
public:
void changeGear(){ cout<<"\nI am Vehicle changeGear() function..\n";}
};
class WaterVehicle : public
Vehicle
{
public:
void Float(){ cout<<"\nI am float function of Water Vehicle";}
};
class LandVehicle : public
Vehicle
{
public:
void Move(){ cout<<"\nI am move function of Land Vehicle"<<endl;}
};
class AmphibiousVehicle : public
LandVehicle,public
WaterVehicle
{
};
int main(int
argc, char *argv[])
{
AmphibiousVehicle
amphibious;
amphibious.Float();
/*Calling Float function of Water Vehicle class*/
amphibious.Move();
/*Calling Move function of Land Vehicle class*/
system("PAUSE");
return 0;
}
|
Output:
I
am float function of Water Vehicle
I
am move function of Land Vehicle
|
Advantage of Multiple Inheritance:
As
was the case with simple (single) inheritance multiple inheritance also decreases
redundant code as we can inherit a class from many classes and can use their
functions without the need to write them again.
However
there are more disadvantages of multiple inheritance, than its advantages.
Problems
with Multiple Inheritance
Increased
complexity
Amphibious
vehicle hierarchy is a complicated as this class is derived from two classes
that will make code more complex and less understandable however this is
obvious as amphibious vehicle is a complicated vehicle. It is generic problem.
Reduced
understanding
Due
to increased complexity of class hierarchy the object model becomes difficult
it understand especially for someone who is looking it first time.
Duplicate
features
As
we are deriving a single class from more than one class so there is a chance of
duplication of features (same methods in both parents), following problems may
arise due to duplicate features,
Problem 1:
Ambiguity
Consider the class
hierarchy of Mermaid class below,
As mermaid also need to eat and its both parents
have their own methods of eating so here question arises,
Which eat operation
Mermaid should inherit as both functions are available?
Solution – We can solve
this problem by explicitly calling eat method from any of the parent classes in
Mermaid class according to behaviour of Mermaid (i.e. if it eats like a Woman
we can call eat method of Woman class and if eats like Fish we can call method
of Fish class), for this we will Override the Common method in multiply
inherited class and in that class overridden method we will call the
appropriate base class function.
Example C++ Code
#include <iostream>
#include <stdlib.h>
using namespace std;
/*Program to demonstrate simple multiple inheritance*/
class Fish
{
public:
void eat(){
cout<<"\n In Fish eat method
";
}
};
class Woman
{
public:
void eat(){
cout<<"\n In Woman eat method
\n"<<endl;
}
};
class Mermaid : public Woman,public
Fish
{
public:
void eat(){
cout<<"\n In Mermaid eat
method "<<endl;
cout<<"\n Explicity calling
Woman eat method...."<<endl;
Woman::eat();
}
};
int main(int
argc, char *argv[])
{
Mermaid mermaid;
/*This Mermaid object will have two implicit objects one of Fish class
and one of
Woman class*/
mermaid.eat();
/*Calling Mermaid eat method*/
system("PAUSE");
return 0;
}
|
Problem 2: Two
instances for same function (Diamond Problem)
Here
Amphibious Vehicle will have two copies of changeGear function as it will have
two objects of Vehicle class one with respect to Land Vehicle and one with
respect to Water Vehicle as shown below,
Actual Memory Layout
Compiler
will not be able to decide which changeGear operation Amphibious Vehicle should
inherit so it will generate an error as shown below (two copied of same method),
error: request for member
`changeGear' is ambiguous
error: candidates are: void Vehicle::changeGear()
void Vehicle::changeGear()
Execution terminated
|
Solution
to Diamond Problem
Some
languages disallow diamond hierarchy
Others
provide mechanism to ignore characteristics from one side. There are two cases
while solving diamond problem virtual inheritance and non virtual inheritance
(we will study these details in coming lectures)
01.2. Kinds of
Association:
There are two main types of
association which are then further subdivided i.e
1.
Class Association
2.
Object
Association
- Class
Association
Class association is implemented in terms
of Inheritance. Inheritance implements generalization/specialization
relationship between objects. Inheritance is considered class association.
- In
case of public inheritance it is “IS-A” relationship.
- In
case of private inheritance it is “Implemented in terms of” relationship.
This relationship ensures that public
members of base class are available to derived class in case of public
inheritance.
When we create objects of classes in which
we have implemented inheritance relationships we are forcing the inheritance
relationship between created objects. In this case the derived class objects
will also contain base class objects attributes and methods.
- Object
Association
It
is the interaction of stand alone objects of one class with other objects of
anther class.
It
can be of one of the following types,
·
Simple
Association
·
Composition
·
Aggregation
The
two interacting objects have no intrinsic relationship with other object. It is
the weakest link between objects. It is a reference by which one object can
interact with some other object.
Customer
gets cash from cashier
Employee
works for a company
Ali
lives in a house
Ali
drives a car
It
is generally called as “association”
instead of “simple association”
Kinds of Simple Association
Simple association can be categorized in
two ways,
- With
respect to direction (navigation)
- With
respect to number of objects (cardinality)
Kinds of Simple Association w.r.t
Navigation
With respect to navigation association has
the following types,
- One-way
Association
- Two-way
Association
- One-way
Association
In
One way association we can navigate along a single direction only, it is denoted
by an arrow towards the server object.
Examples:
·
Ali
lives in a House
·
Ali
drives his Car
- Two-way
Association
In
two way association we can navigate in both directions, it is denoted by a line
between the associated objects
Examples:
Employee
works for company
Company
employs employees
Two-way
Association - Example
Yasir
is a friend of Ali
Ali
is a friend of Yasir
Kinds of Simple
Association w.r.t Cardinality
With
respect to cardinality association has the following types,
- Binary
Association
- Ternary
Association
- N-ary
Association
a. Binary Association
It associates
objects of exactly two classes; it is denoted by a line, or an arrow between
the associated objects.
Example
Association
“works-for” associates objects of exactly two classes
Association
“drives” associates objects of exactly two classes
b. Ternary Association
It
associates objects of exactly three classes; it is denoted by a diamond with
lines connected to associated objects.
Example
Objects
of exactly three classes are associated
c. N-ary Association
An
association between 3 or more classes its practical examples are very rare.
An
object may be composed of other smaller objects, the relationship between the
“part” objects and the “whole” object is known as Composition, Composition is
represented by a line with a filled-diamond head towards the composer object
Example – Composition of Ali
Example – Composition of Chair
Composition is stronger relationship:
Composition
is a stronger relationship, because
Composed
object becomes a part of the composer
Composed
object can’t exist independently
Example I
Ali
is made up of different body parts
They
can’t exist independent of Ali
Example II
Chair’s
body is made up of different parts
They
can’t exist independently
An
object may contain a collection (aggregate) of other objects, the relationship
between the container and the contained object is called aggregation, Aggregation
is represented by a line with unfilled-diamond head towards the container
Example
– Aggregation
Example
– Aggregation
Aggregation
is weaker relationship
Aggregation
is weaker relationship, because
·
Aggregate
object is not a part of the container
·
Aggregate
object can exist independently
Example
I
Furniture
is not an intrinsic part of room
Furniture
can be shifted to another room, and so can exist independent of a particular
room
Example
II
A
plant is not an intrinsic part of a garden
It
can be planted in some other garden, and so can exist independent of a
particular garden
class member functions are also called class methods
Post a Comment
Don't Forget To Join My FB Group VU Vicky
THANK YOU :)