Java Inheritance
In this tutorial, we will learn about the inheritance concept and a working example of inheritance with Java and how Java takes care of a diamond problem.
What is an inheritance
Inheritance is the concept of acquiring data properties and methods of a super class as a child class.
Inheritance in Java
Inheritance in java can be achieved in two different ways.
- Extending a class.
- Extending interfaces.
Prior to Java 8, it was never exposed to the diamond problem as extending multiple classes is restricted. From 8 onwards, having interface with default methods along with implementation leading to the diamond problem.
Diamond problem is the ambiguity of deciding which method to execute when child class extends two classes but both the classes contain same method with same signature.
In Java 8 also extending multiple classes is restricted but in case of interfaces it is possible to extend two or more interfaces prior to 8 also but default method in interface is introduced from 8.
Inheritance by extending a class:
For inheriting properties and methods of a class X requires class Y to extend class X using keyword extends.
Example1:
class X
{
private String property="X Class Property";
public String getProperty()
{
return property;
}
}
public class Y extends X {
public static void main(String[] args) {
Y child =new Y();
System.out.println(child.getProperty());
}
}
Output:
X Class Property
After executing the example1 above line gets printed, explanation as follows.
- Y class extended class X
- Will have all access of class X members and methods.
- Y class accessed X class method, and prints value of X class member.
Example2:
class X
{
private String property="X Class Property";
public String getProperty()
{
return property;
}
public void setProperty(String property) {
this.property= property;
}
}
public class Y extends X {
public static void main(String[] args) {
Y child =new Y();
child.setProperty("X Class Property value changed by Y class");
System.out.println(child.getProperty());
}
}
Output:
X Class Property value changed by Y class
Explanation, X class data member value will be accessed by Y as it is a sub class and can change the data member values of super class.
Inheritance by extending an interface:
Till version 8, Java never exposed to the diamond problem. In version 8 introduced a new feature of having default methods in the interface, while extending two or more interfaces which are having default method of same signature can lead to diamond problem.
To avoid a diamond problem, extending interface should have the same default method with same signature otherwise compilation will fail.
Example :
public interface childInterface extends ParentA,ParentB{
}
interface ParentA {
public default void method() {
System.out.println("ParentA Method");
}
}
interface ParentB {
public default void method() {
System.out.println("ParentB Method");
}
}
The above example will not compile, extending interface ChildInterface should override the default method of with same signature as follows.
interface ChildInterface extends ParentA,ParentB{
@Override
default void method() {
//method is written here.
}
}
interface ParentA {
public default void method() {
System.out.println("ParentA Method");
}
}
interface ParentB {
public default void method() {
System.out.println("ParentB Method");
}
}
Conclusion:
In this tutorial, we have learned about inheritance and how to achieve in Java with the working examples and covered on diamond problem and how java handles this diamond problem.