y
nl
O
se
U
tre
en
Session: 5 C
h
ec
Multiple Inheritance and
pt
rA
Interfaces
Fo
Version 1.0 © 2012 Aptech Limited.
y
nl
Describe Multiple Inheritance
O
se
List the problems associated with Multiple Inheritance
U
Describe Interface
tre
en
Explain Multiple Inheritance using Interfaces
C
Explain constructor execution in Multiple Inheritance
h
ec
pt
rA
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 5 2
y
nl
O
Emphasizes the correct way of expressing the
se
relationships between classes
U
tre
en
Explains creation of a class by combination of other
classes
C
h
ec
pt
Describes the use of interfaces to achieve multiple
rA
inheritance in programming languages
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 5 3
y
nl
It is a technique in which one class inherits its
O
properties from more than one classes.
se
The figure shows an example of multiple inheritance.
U
tre
en
C
h
ec
pt
rA
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 5 4
y
nl
The figure shows the class diagram of the inheritance
O
hierarchy created by the three classes namely
se
Herbivore, Carnivore, and Omnivore.
U
tre
en
C
h
ec
pt
rA
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 5 5
y
nl
The Diamond Problem - a class inherits from two
O
parent classes each of which in turn inherit from a
se
common super class.
U
The figure depicts the diamond problem.
tre
en
C
h
ec
pt
rA
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 5 6
y
nl
Ambiguity of Name - a base classes have a method
O
with the same name and signature.
se
The figure depicts the name ambiguity problem.
U
tre
en
C
h
ec
pt
rA
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 5 7
y
nl
An interface only declares the signature of the
O
methods it wishes to expose.
se
Syntax
U
tre
<visibility-modifier> interface
en
<interface-name>
{
C
// abstract method declarations
h
ec
}
pt
rA
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 5 8
y
nl
The following table shows features that differentiate a
O
class and an interface.
se
Class Interface
U
A class can be instantiated An interface cannot be instantiated
tre
A class can have concreted methods, An interface cannot have concrete
that is, methods with a body methods
en
A child class may not override all A class implementing an Interface must
methods of a parent class
Multiple classes cannot be inherited by a
C implement all methods of an interface
Multiple interfaces can be implemented by
h
ec
child class to simulate multiple a class to simulate multiple inheritance
inheritance
pt
An ‘is-a’ relationship is required to An ‘is-a’ relationship is not mandatory for
rA
implement inheritance between classes implementing an interface
Methods of an interface are implicitly Methods of a class can have any access
Fo
public modifier
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 5 9
y
nl
Some advantages of interface are as follows:
O
se
Allows simulating multiple inheritance of classes
U
Helps to avoid ambiguity between the methods of the
tre
different classes as seen in C++
en
C
Allows combining features of two or more interfaces
h
such that a class needs to only implement the
ec
combined result
pt
rA
Helps to hide an inherited member name from any
Fo
code outside the derived class
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 5 10
y
nl
O
se
Parent Class
U
tre
No-argument constructor: Invoked in the sequence in
en
which the child class has inherited the parent classes
C
h
ec
Parameterized constructor: User can handle by invoking
the parent class constructor from the constructor of the
pt
child class and pass arguments to the constructor
rA
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 5 11
y
nl
Multiple Inheritance is a technique in which one class inherits its
O
properties from more than one classes.
se
Diamond Problem is a situation that arises when a class inherits
U
from two parent classes each of which in turn inherit from a
tre
common super class.
en
An interface is a type similar to a class that consists of only
C
method declarations without any implementation.
h
ec
A class can implement more than one interface to simulate
pt
multiple inheritance.
rA
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 5 12
y
nl
Invocation of constructors in multiple inheritance depends on
O
the sequence in which the child class invokes them in its own
se
constructor.
U
Ambiguity of name is a condition in multiple inheritance
tre
wherein the base classes have a method with the same name
en
and signature.
C
h
ec
pt
rA
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 5 13