Inheritance in Java

Inheritance in Java

 
Inheritance is the approach of sharing instance variables and methods of a class with other class(es), which means inheriting features and behaviours of a class to other class(es). Many times, multiple objects may have some common characteristics. For example, Car A shares its characteristics (Registration Number, Current Speed, Current Gear, Number of Cylinders) with Car B and Car C. And both Car B and Car C have some additional features: Car B has a Rear Camera and Car C has Front Fog Lights, these additional features make Car A, Car B and Car C different from each other. In this case, Car A becomes the Superclass of Car B and Car C, and Car B and Car C are the Subclasses of Car A.
 
Inheritance in Java  
 
In Java programming, A class can have one-directional Superclass and each Superclass might have an unlimited number of Subclasses. Each subclass is a specialized version of its superclass.
Inheritance takes place when the definition of a class is integrated into another class; to do so in Java, you need to use the extends keyword. For example:
 
Code
  1. public class Inheritance  
  2. {  
  3.     public static void main(String[] args) 
  4.     {  
  5.         Math2 obj = new Math2();  
  6.         obj.Add(1020);  
  7.         obj.Subtract(8070);  
  8.     }  
  9. }  
  10.   
  11. class Math1   
  12. {  
  13.     int result;  
  14.   
  15.     public void Add(int a, int b) 
  16.     { // Add two integers  
  17.         result = a + b;  
  18.         System.out.println(result);  
  19.     }  
  20. }  
  21.   
  22. class Math2 extends Math1  
  23. {  
  24.     public void Subtract(int a, int b) 
  25.     { //Subtract a small integer from big one  
  26.         if (a >= b) 
  27.          {  
  28.             result = a - b;  
  29.         } else {  
  30.             result = b - a;  
  31.         }  
  32.         System.out.println(result);  
  33.     }  
  34. }  
Output
 
30
10
 
In the above example, Math2 extends Math1. Therefore, the variable "result" is used in Math2 without declaring it in Math2. And the Add() method of Math1 is called using the object of class Math2. It happens because the definition of Math1 is integrated into Math2 by using the extends keyword.
 

Types of Inheritance

 

1. Single Level Inheritance

 
In a single level of inheritance, one class extends one class only. The above-written program is a good example of a single level inheritance.
 
Single Level Inheritance in Java 
 

2. Multilevel Inheritance

 
Multilevel Inheritance takes place when a class extends a subclass.
 
MultiLevel Inheritance in Java 
 
There could be 'n' number of levels in Multilevel Inheritance. Therefore, the lowermost class has a definition of all the above classes. The below-written program demonstrates Multilevel Inheritance.
  
Code
  1. public class MultilevelInheritance   
  2. {  
  3.     public static void main(String[] args) 
  4.     {  
  5.         C obj = new C();  
  6.         obj.method1();  
  7.         obj.method2();  
  8.         obj.method3();  
  9.     }  
  10. }  
  11.   
  12. class A 
  13. {  
  14.     public void method1() 
  15.     {  
  16.         System.out.println("You are in method1");  
  17.     }  
  18. }  
  19.   
  20. class B extends A 
  21. {  
  22.     public void method2() 
  23.     {  
  24.         System.out.println("You are in method2");  
  25.     }  
  26. }  
  27.   
  28. class C extends B 
  29. {    
  30.     public void method3() 
  31.      {  
  32.         System.out.println("You are in method3");  
  33.     }  
  34. }  
Output
 
You are in method1
You are in method2
You are in method3
 

3. Hierarchical Inheritance

 
When two or more classes are derived from a superclass, is called Hierarchical Inheritance.
 
Hierarchical Inheritance in Java 
 
Code
  1. public class HierarchicalInheritance 
  2. {    
  3.     public static void main(String[] args) 
  4.     {  
  5.         B obj = new B();  
  6.         obj.method1();  
  7.         obj.method2();  
  8.   
  9.         C obj1 = new C();  
  10.         obj1.method1();  
  11.         obj1.method3();  
  12.     }  
  13. }  
  14.   
  15. class A   
  16. {  
  17.     public void method1() 
  18.     {  
  19.         System.out.println("You are in method1");  
  20.     }  
  21. }  
  22.   
  23. class B extends A  
  24. {  
  25.     public void method2() 
  26.     {  
  27.         System.out.println("You are in method2");  
  28.     }  
  29. }  
  30.   
  31. class C extends A   
  32. {  
  33.     public void method3() 
  34.     {  
  35.         System.out.println("You are in method3");  
  36.     }  
  37. }  
Output
 
You are in method1
You are in method2
You are in method1
You are in method3
 

4. Multiple Inheritance (Not Supported by Java)

 
In Multiple Inheritance, a subclass has many superclasses, which means a class extends more than one class.
 
Multiple Inheritance in Java 
 
Note: Multiple Inheritance is partially achieved by using Interface.
 

Interface in Java

 
An interface is a container of abstract methods. It allows Java to implement Multiple Inheritance because a class can't have more than one superclass in Java but can implement many interfaces. Methods are just declared in the interface, but not defined. The class which implements an interface must have to define the method declared in the interface. Access modifiers and return type must be the same as declared in the interface. Private and static methods can't be declared in the interface.
 
Code
  1. interface Intr   
  2. {  
  3.     public void IntrMethod();  
  4.   
  5.     public void IntrMethod1();  
  6. }  
  7.  
  8. interface Intr1  
  9. {  
  10.     public void Intr1Method();  
  11. }  
  12.   
  13. interface Intr2   
  14. {  
  15.     public void Intr2Method();  
  16. }  
  17.   
  18. public class Interface 
  19. {  
  20.     public static void main(String[] args) 
  21.     {  
  22.         A obj = new A();  
  23.         obj.IntrMethod();  
  24.         obj.IntrMethod1();  
  25.         obj.Intr1Method();  
  26.         obj.Intr2Method();  
  27.     }  
  28. }  
  29.   
  30. class A implements Intr, Intr1, Intr2   
  31. {  
  32.     public void IntrMethod() 
  33.     {  
  34.         System.out.println("IntrMethod");  
  35.     }  
  36.   
  37.     public void IntrMethod1() 
  38.     {  
  39.         System.out.println("IntrMethod1");  
  40.     }  
  41.   
  42.     public void Intr1Method() 
  43.     {  
  44.         System.out.println("Intr1Method");  
  45.     }  
  46.   
  47.     public void Intr2Method() 
  48.     {  
  49.         System.out.println("Intr2Method");  
  50.     }  
  51. }  
Output
 
IntrMethod
IntrMethod1
Intr1Method
Intr2Method
 
How Access Specifiers affect Inheritance
 
There are four Access Specifiers in Java
  1. Public: When a member of a class is modified by the public specifier, it can be accessed by any other code.
  2. Protected: Protected is only applicable in case of Inheritance. When a member of a class is modified by protected, it can only be accessed by the members of its class or subclass.
  3. Private: A member of a class which is modified by the private specifier, can only be accessed by the member of its class.
  4. Default: When you don't specify an access specifier to a member, Java automatically specifies a default. And the members modified by default can only be accessed by any other code in the package, but can't be accessed outside of a package.

Restricting Inheritance

 
I hope you have learned how to implement various types of inheritance in Java. Now, what to do if you want that no other classes can extend a desired one, which means restricting inheritance.
 
You just need to use the "final" keyword as follows:
 
final class myclass{
// Insert code here
}
 
Now, "myclass" can't be extended by any other class.
 
 

Conclusion

 
In this article, we learned about Inheritance in Java, interfaces in java, various types of inheritance and their java implementation


Similar Articles