×

Multiple Inheritance Programs in Java

A component of the object-oriented notion known as multiple inheritances allows a class to inherit properties from multiple parent classes. When methods that have the same signature are present in both superclasses and subclasses, an issue arises. The method's caller cannot specify to the compiler which class method should be called or even which class method should be given precedence.

NOTE: Multiple Inheritances is not supported by Java.

Example 1:

import java.io.*;
class Parent1 {
void fun() {
	System.out.println("Parent1");
}
}
class Parent2 {
void fun() {
	System.out.println("Parent2");
}
}
class Test extends Parent1,Parent2 {
public static void main(String args[]) {
	Test t = new Test();
	t.fun();
}
}

Output

C:\javap>java Demo.java
Demo.java:12: error: '{' expected
class Test extends Parent1,Parent2 {
                          ^
1 error

Conclusion: As shown in the code above, issues can arise when utilizing the Test object to call the fun() function, such as deciding whether to use Parent1's or Parent2's fun() method.

Example 2:

import java.io.*;
class GrandParent {
void fun() {
	System.out.println("Grandparent");
}
}
class Parent1 extends GrandParent {
void fun() {
	System.out.println("Parent1");
}
}
class Parent2 extends GrandParent {
void fun() {
	System.out.println("Parent2");
}
}
class Test extends Parent1, Parent2 {
public static void main(String args[]) {
	Test t = new Test();
	t.fun();
}
}

Output

C:\javap>javac Demo.java
Demo.java:17: error: '{' expected
class Test extends Parent1, Parent2 {
                          ^
1 error

The fun() method throws a compiler error once more because multiple inheritances, which are permitted in other languages like C++, lead to a diamond problem. The code reveals the following: Problems arise when the method fun() is called using the Test object, such as when deciding whether to call the fun() method of Parent1 or Parent2. Java consequently does not allow multiple class inheritances in order to prevent these issues.

Java classes do not enable multiple inheritances, and managing the complexity brought on by multiple inheritances is exceedingly difficult. It causes issues with a number of processes, such as casting and function Object() { [native code] } chaining, and the main reason is that multiple inheritancesare rarely necessary; therefore, it is preferable to do without it to make things clear-cut and simple.

How do Default Methods & Interfaces Solve the Aforementioned Issues?

Java 8 allows interfaces to give a default implementation of methods, and Java 8 supports this. Additionally, a class may support two or more interfaces. If the default methods in both implemented interfaces have the same method signature, the implementing class must either use the super keyword to explicitly specify which default method is to be used in a method other than main(), override the default method in the implementing class, or specify which standard method is to be used in the default overridden method of the effective implementation class.

Example 3:

interface P1{
	default void show()
	{
		System.out.println("Default P1");
	}
}
interface P2 {
	default void show()
	{
		System.out.println("Default P2");
	}
}
class TestClass implements P1, P2 {
	public void show()
	{
		P1.super.show();
		P2.super.show();
	}
	public void showOfP1() {
		P1.super.show();
	}
	public void showOfP2() {
		P2.super.show(); 
	}
	public static void main(String args[])
	{
		TestClass d = new TestClass();
		d.show();
		System.out.println("Now Executing showOfP1() showOfP2()");
		d.showOfP1();
		d.showOfP2();
	}
}

Output

interface P1{
	default void show()
	{
		System.out.println("Default P1");
	}
}
interface P2 {
	default void show()
	{
		System.out.println("Default P2");
	}
}
class TestClass implements P1, P2 {
	public void show()
	{
		P1.super.show();
		P2.super.show();
	}
	public void showOfP1() {
		P1.super.show();
	}
	public void showOfP2() {
		P2.super.show(); 
	}
	public static void main(String args[])
	{
		TestClass d = new TestClass();
		d.show();
		System.out.println("Now Executing showOfP1() showOfP2()");
		d.showOfP1();
		d.showOfP2();
	}
}

NOTE: There is a compiler issue if the default method implementation is removed from "TestClass". There won't be a problem if none of the intermediary interfaces implement the root interface if there is a diamond through an interface. If they offer implementation, it can be obtained via the super keyword as described above.

Example 4:

interface GP1 {
	default void show()
	{
		System.out.println("Default GP1");
	}
}
interface P1 extends GP1 {
}
interface P2 extends GP1{
}
class TestClass implements P1, P2 {
	public static void main(String args[])
	{
		TestClass d = new TestClass();
		d.show();
	}
}

Output

Default Gp1

Example 5:

interface Backend {
  public void connectServer();
}


class Frontend {
  public void responsive(String str) {
System.out.println(str + " is utilized as a front-end language.");
  }
}
class Language extends Frontend implements Backend {


  String language = "Java";
  public void connectServer() {
System.out.println(language + " is utilized as the backend language.");
  }


  public static void main(String[] args) {
    Language java = new Language();
java.connectServer();
java.responsive(java.language);
  }


}

Output

java is utilized as the backend language.
Java is utilized as a front-end language.

Related Topics

Traverse through HashMap in Java

In this tutorial, we will discuss traversing through HashMap in Java Introduction: HashMap<Key, Value> is a part of the java collection implemented from the java 1.2 version. HashMap is written as HashMap<K,...

4 minutes read.

Diamond problem in Java

The Diamond Problem in Java is connected to multiple inheritances. It is also referred to as the "deadly diamond dilemma" or even the "deadly diamond of death”. The solution for...

5 minutes read.

Difference between Aggregation and Composition in Java

What is Aggregation? Before we start discussing Aggregation, we have to know some general information about our classes in java. Generally we have two members in our classes , the first...

8 minutes read.

Java Beans

It is a Java class, It follows conventions they are: It must have a no-arg constructorIt must be serializable.It must provide the methods to get and set the properties Uses Of Java...

3 minutes read.

Difference between C, C++, java

C Language: C language is a procedure oriented language. It has been invented by Dennis Ritchie in the year 1970. It is one of the computer programming language. The purpose of...

3 minutes read.

How to take Array Input in Java

In this tutorial, we will learn about how to take array input in Java. So, before taking inputs let us know what an array is first. Array: An array is a collection...

10 minutes read.

Java Wrapper Class

Wrapper class in Java encapsulates a primitive type within an object. In other words, it is a unique mechanism of Java to convert primitive type in to object and object into primitive type....

3 minutes read.

Reverse a String in Java

Reversing a string means that if we have a string called “what is your name”, the reversed format is “eman ruoy si tahw”. Reversing a string involves totally flipping the...

3 minutes read.

Java Hello World

Let’s start by writing a simple program that prints “Hello World” to the output window. Write the program into any text editor or IDE (Eclipse, Netbeans, etc.) and save the file with the...

2 minutes read.

Java HashSet

HashSet implements the set interface. It uses the hash table to make the collection to store different data types. The hash set is the unordered collection of different data types....

6 minutes read.

How to Set Environment Variables for Java

Introduction Java is an object-oriented programming language that is based on classes and can be employed mostly to develop web and desktop applications. No matter the computer architecture, Java applications are...

7 minutes read.

What is the ambiguity problem in Java?

The ambiguity problem in Java occurs when a method or constructor is overloaded with two or more methods with the same name but different parameters. This can confuse when multiple...

6 minutes read.

Recursion Program in Java

The recursion program in Java demonstrates the usage of recursion. The process by which a function/ method calls itself, again and again, is called recursion. Each recursive call is pushed...

10 minutes read.

Java Map Example

In Java, the Map is an interface that is used mainly to denote key and value pairs. The central concept and theme of this mapping in the java collection framework...

4 minutes read.

Java Trim

Leading and leaving spaces are removed by the built-in Java String trim() method. Space seems to have the Unicode element of "x0040." Java trim() function looks for all of these...

3 minutes read.

Java Developer

Who is a Java Developer? A Java developer is a skilled programmer who works on commercial applications, software, and webpages.  Java developers can work in two different areas: Operating system development:...

3 minutes read.

Best Java Security Framework

The security of applications is currently our top concern when creating them. The applications or bits of code running over the network are exposed to dangers and may jeopardize integrity,...

3 minutes read.

Java Integer min() method

The min()  method of Integer class returns the smaller of two int values. It returns the same result as by calling Math.min.  Syntax public static int min(int a, int b) Parameters The parameters ‘a’...

2 minutes read.

Balanced Prime Number in Java

This section will cover the definition of a balanced prime number as well as how to find one using a Java program. Balance Prime Number A prime number that is equivalent to...

5 minutes read.

Catalan number in Java

In general mathematics, Catalan numbers can be defined as the sequence of natural numbers that frequently occur in counting problems often encountered in recursively defined objects. Mathematical formula of Catalan number Coming...

3 minutes read.