Java/J2EE and SOA (305 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (45 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (304 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

How To Implement Multiple Inheritance In Java?

Published on Aug 14,2019 312 Views
4 / 13 Blog from Java OOPS

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

This article will help you implement a concept that is otherwise not possible to implement in Java. I am referring to Multiple Inheritance In Java. Following pointers will be touched upon in this article,

So let us get started with this Multiple Inheritance in Java article,

Multiple Inheritance In Java

Object Oriented Programming provides a user the feature of multiple inheritance, wherein a class can inherit the properties of more than a single parent class. In simpler terms, multiple inheritance means a class extending more than one class.

The programming language of java is unable to utilise this feature directly. It can be achieved indirectly through the usage of interfaces.

Moving on with this Multiple Inheritance in Java article,

Sample Program

In the following example, we have two interfaces : Motorbike and Cycle. Motorbike interface consists of the attribute speed. The method is totalDistance(). Cycle interface consists of the attribute distance() and the method speed().

Both these interfaces are implemented by the class TwoWheeler.

interface MotorBike
{
int speed=50;
public void totalDistance();
}
interface Cycle
{
int distance=150;
public void speed();
}
public class TwoWheeler implements MotorBike,Cycle
{
int totalDistance;
int avgSpeed;
public void totalDistance()
{
totalDistance=speed*distance;
System.out.println("Total Distance Travelled : "+totalDistance);
}
public void speed()
{
int avgSpeed=totalDistance/speed;
System.out.println("Average Speed maintained : "+avgSpeed);
}
public static void main(String args[])
{
TwoWheeler t1=new TwoWheeler();
t1.totalDistance();
t1.speed();
}
}

Output

Total Distance Travelled : 7500

Average Speed maintained : 150

The program given above avoids ambiguity even when classes are used instead of interfaces. However, Java does not support it. When both the classes have the same method in it, the compiler is unable to decide upon the method to be called. Using interface avoids this ambiguity as the methods of interface are abstract by default.

Moving on with this Multiple Inheritance in Java article,

Multiple Inheritance Without Ambiguity

interface InterfaceOne
{
public void disp();
}
interface InterfaceTwo
{
public void disp();
}
public class Main implements InterfaceOne,InterfaceTwo
{
@Override
public void disp()
{
System.out.println("display() method implementation");
}
public static void main(String args[])
{
Main m = new Main();
m.disp();
}
}

Output

display() method implementation

The Main method implements both the interfaces i.e. InterfaceOne and InterfaceTwo. It executes without any ambiguity.

Let’s take a look at another example for a better understanding of multiple inheritance:

interface Sing

{

default void singRock(){

System.out.println(“I am singing rock”);

}

}

interface Dance

{

default void danceSlow(){

System.out.println(“I am dancing slow!”);

}

}

public class Human implements Sing, Dance

{

public static void main(String[] args)

{

Human h = new Human();

h.singRock();

h.danceSlow();

}

}

Output

I am singing rock

I am dancing slow!

Thus, multiple inheritance can be achieved by the methods discussed in this article.

Thus we have come to an end of this article on ‘Multiple Inheritance in Java’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this blog  and we will get back to you as soon as possible.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.