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

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (345 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

How To Implement Multiple Inheritance In Java?

Last updated on Jun 17,2021 26.1K Views

4 / 14 Blog from Java OOPS

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 Online Course 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.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 4th May,2024

4th May

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 25th May,2024

25th May

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How To Implement Multiple Inheritance In Java?

edureka.co