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.

Example 1– Multiple Inheritance

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:


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:


Output:


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:


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:

Output:


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


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),


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

  1. 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.

  1. 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,

  1. One-way Association
  2. Two-way Association

  1. 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


  1. 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,

  1. Binary Association
  2. Ternary Association
  3. 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 :)

Previous Post Next Post