
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to solve diamond problem using default methods in Java
Inheritance is a relation between two classes where one class inherits the properties of the other class. This relation can be defined using the extends keyword as −
public class A extends B{
}
The class which inherits the properties is known as sub class or, child class and the class whose properties are inherited is super class or, parent class.
In inheritance a copy of super class members is created in the sub class object. Therefore, using the sub class object you can access the members of the both classes.
Multiple inheritance is where one class inherits the properties of multiple classes. In other words, in multiple inheritance we can have one child class and n number of parent classes. Java does not support multiple inheritance (with classes).
The diamond problem
- For instance, let us assume that Java does support multiple inheritance.
- And if we have an abstract class named Sample with an abstract method demo().
- Then if two other classes in the same package extends this class and try to implement its abstract method, demo().
- Then, as per the basic rule of inheritance, a copy of both demo() methods should be created in the subclass object which leaves the subclass with two methods with same prototype (name and arguments).
- Then, if you call the demo() method using the object of the subclass compiler faces an ambiguous situation not knowing which method to call. This issue is known as diamond problem in Java.
Solution to diamond problem
You can achieve multiple inheritance in Java, using the default methods (Java8) and interfaces.
From Java8 on wards default methods are introduced in an interface. Unlike other abstract methods these are the methods of an interface with a default implementation. If you have default method in an interface, it is not mandatory to override (provide body) it in the classes that are already implementing this interface.
You can have same default methods (same name and signature) in two different interfaces and, from a class you can implement these two interfaces.
If you do so, you must override the default method from the class explicitly specifying the default method along with its interface name.
Example
interface MyInterface1{ public static int num = 100; public default void display() { System.out.println("display method of MyInterface1"); } } interface MyInterface2{ public static int num = 1000; public default void display() { System.out.println("display method of MyInterface2"); } } public class InterfaceExample implements MyInterface1, MyInterface2{ public void display() { MyInterface1.super.display(); //or, MyInterface2.super.display(); } public static void main(String args[]) { InterfaceExample obj = new InterfaceExample(); obj.display(); } }
Output
display method of MyInterface1 display method of MyInterface2
- Related Articles
- How to solve the diamond problem using default methods in Java?
- Default methods in Java
- What is diamond problem in case of multiple inheritance in java?
- Java Program to Solve Travelling Salesman Problem using Incremental Insertion Method
- Java 8 default methods in interfaces
- Java Program to Solve Set Cover Problem
- What are Default Methods in Java 8?
- Can we override default methods in Java?
- C++ Program to Solve Knapsack Problem Using Dynamic Programming
- What is the use of default methods in Java?
- Python Program to solve Maximum Subarray Problem using Kadane’s Algorithm
- How we Can Solve this Problem
- C++ program to Solve Tower of Hanoi Problem using Binary Value
- Python Program to solve Maximum Subarray Problem using Divide and Conquer
- Is it mandatory to override the default methods of an interface in Java?
