The diamond problem in object oriented programming can be defined as an enigma which aroused as a result of multiple inheritance. Object oriented programs like C++ allow multiple inheritance in state which will result in diamond problem. Java does not allow multiple inheritance for classes but it is enabled to interfaces.
What is Diamond Problem?
Consider two classes X and Y derived from a class whose name is W. Another class with name Z is derived from classes X and Y using the process of multiple inheritance. The problem with this kind of inheritance hierarchy is when the class Z is instantiated; objects belonging to class Z will also be instantiated. The definitions of methods and calls present in class W will be unclear because the class is not sure to choose between derived classes X and Y.
Does Java have Diamond Problem?
Since Java does not use multiple inheritance, there is no diamond problem in Java. Java uses interface method to simulate the multiple inheritance process. The use of interface is similar to that of multiple inheritance. Unlike multiple inheritance, interface is implemented singly. This single implementation of interface will avoid diamond problem.
What is Interface?
Now we know that interface is used in Java instead of multiple inheritance. Interface may resemble like class but it is not a class. Interface has methods and variables like class but these methods are declared as abstract of the interface by default. All the variables declared in interface are static and final by default.
Interfaces are used only for abstraction of methods and variables. But methods present in interface don’t have body so implementation is required before accessing them. Unlike multiple inheritance, interfaces cannot be extended more than one class.
Interface Declaration
Interfaces can be declared using specific keyword, “interface”. Consider the following program as an example
interface MyInterface
{
/* All the methods are public abstract by default
* Note down that these methods are not having body
*/
public void method1();
public void method2();
}
In the above program the interface “MyInterface” is declared using the keyword “interface”.
Implementation of Interface
A class implements an interface by providing all the methods body which are declared in the interface. A class may implement an interface but only an interface can extend into another interface.
Difference between Interface and Abstract Class
The interface process may look similar to abstract class but there are lot of differences
- Interface can support multiple inheritance while multiple inheritance is not supported in abstract class.
- The keyword “implement” is used during inheritance in interface while abstract class uses “extends” keyword during inheritance.
- Interface only uses abstract methods while abstract class can use both abstract and concrete methods.
- Only public methods are used in interface whilst all methods except private are used by abstract class.
- No need to state access specifier interface, it is considered as public by default. Abstract class requires specify the type of access specifier used.
- When the access modifier is not mentioned in interface it is taken as abstract by default. Specification of access modifier is compulsory in abstract class.
- Interface variables should be public, static or final. Abstract class variables can be of any type including private.
- Constructors are not allowed in interface. Abstract class requires the constructors to be mentioned along with the code.
- Interface does not support the inclusion of main() method. Abstract class support main() method when it is included along with the code.