Multiple Inheritance
Through interfaces
• Multiple Inheritance:
▫ One class extending more than one classes, which
means a child class has two or more parent classes.
▫ Java doesn’t support multiple inheritance
▫ For example class C extends both
classes A and B
▫ We have to use Interface
to implement multiple inheritance
in java
Why multiple inheritance is not supported in java?
• To reduce the complexity and simplify the language, multiple
inheritance is not supported in java.
• Consider a scenario where A, B, and C are three classes.
• The C class inherits A and B classes.
• If A and B classes have the same method and you call it from
child class object, there will be ambiguity to call the method
of A or B class resulting in Diamond problem.
• Since compile-time errors are better than runtime errors, Java
renders compile-time error if you inherit 2 classes. So
whether you have same method or different, there will be
compile time error.
Code example of Multiple Inheritance
class A{
void msg(){System.out.println("Hello");}
}
class B{
void msg(){System.out.println("Welcome");}
}
class C extends A,B{//suppose if it were
public static void main(String args[]){
C obj=new C();
obj.msg();//
Now which msg() method would be invoked?
}
}
--Output
Compile Time Error
How to implement multiple inheritance in java?
• Introduction to interfaces:
▫ Like a class, an interface can have methods and
variables, but the methods declared in interface are by
default abstract (only method signature, no body).
▫ Interfaces specify what a class must do and not how. It
is the blueprint of the class.
▫ Syntax
interface <interface_name> {
// declare constant fields
// declare methods that are abstract by default.
}
• To declare an interface, use interface keyword. It is
used to provide total abstraction.
• It means all the methods in interface are declared with
empty body and are public and all fields are public,
static and final by default.
• Java 8 allows method declared as default to have
method body.
• A class that implement interface must implement all
the methods declared in the interface.
• To implement interface use implements keyword.
• Why do we use interface ?
▫ It is used to achieve total abstraction.
▫ Since java does not support multiple inheritance in case
of class, but by using interface it can achieve multiple
inheritance .
▫ Interfaces are used to implement abstraction. So the
question arises why use interfaces when we have
abstract classes?
The reason is, abstract classes may contain non-final
variables, whereas variables in interface are final, public
and static.
• Code Example
// A simple interface
interface Player
{
final int id = 10;
int move();
}
To implement an interface we use keyword: implements
Java program to demonstrate working of interface.
// A class that implements interface.
import java.io.*;
class testClass implements in1
{
// A simple interface // Implementing the capabilities of
interface in1 // interface.
public void display()
{
{
// public, static and final System.out.println("Geek");
final int a = 10; }
// Driver Code
// public and abstract
public static void main (String[] args)
void display(); {
} testClass t = new testClass();
t.display();
System.out.println(a);
}
}
--Output
Geek 10
Java program to demonstrate multiple
inheritance through default methods.
// A simple Java program to interface Internet
demonstrate multiple {
// inheritance through default // Default method
methods. default void Call()
interface TeleCom
{
{
// default method
System.out.println("Calling over
default void Call() internet");
{
}
System.out.println("Calling
public abstract void Chat();
through TeleCom");
}
}
public abstract void SMS();
} //Cont...
public void SMS()
// Implementation class code
{
class TestClass implements TeleCom, Internet
{
System.out.println("SMS using TeleCom
// Overriding default show method Network");
public void Call() }
{ public void Chat()
// use super keyword to call the show {
// method of TeleCom interface System.out.println("Chat using Internet
System.out.println("Connecting using Network");
Telecom Exchange Network"); }
TeleCom.super.Call();
// use super keyword to call the show
// method of Internet interface public static void main(String args[])
System.out.println("Connecting using Internet {
Network");
TestClass d = new TestClass();
Internet.super.Call();
d.Call();
}
d.SMS();
d.Chat();
//Cont...
}
• Output
Connecting using Telecom Exchange Network
Calling through TeleCom
Connecting using Internet Network
Calling over internet
SMS using TeleCom Network
Chat using Internet Network
// A simple Java program to demonstrate how // Implementation class code
diamond
class TestClass implements PI1, PI2
// problem is handled in case of default
methods {
public static void main(String args[])
interface GPI {
{ TestClass d = new
// default method TestClass();
default void show() d.show();
{
}
System.out.println("Default GPI");
}
}
}
interface PI1 extends GPI { } //output
interface PI2 extends GPI { }
Default GPI