Inheritance in Java is the process of acquiring the properties of one class object into another class object.
It allows the hierarchical classification. Inheritance also provides the facility of reusability.
In Java programming, a class that is inherited is called a superclass or sub class or parent class and class that does the inheriting is called sub class or child class.
If we want to inherit a class, we simply incorporate the definition of one class into another class by using extends keyword.
General form of a class declaration that inherits a super class
1 2 3 | class subClass <strong>extends </strong> superClass{ <br> } |
Type of Inheritance in Java
There are following type of inheritance
- Single level Inheritance
- Multi-level Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
Note: Java does not support multiple inheritance.
Single level Inheritance
Property of single level inheritance is as below
- One Super class and one subclass
- Subclass inherits the features of superclass
Write a program for single-level inheritance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | class Parent // create a super class { int x ,y; void showA() { System.out.println("x=" +x+" y=" + y); } } class Child extends Parent // create a sub class { int z; void showB() { System.out.println("x=" +x + " y=" + y + " z=" +z ); } /* in sub class, member of super class can be accessed directly without any object. */ } public class Test { public static void main(String arg[]) { Child obj = new Child(); obj.x = 10; // subclass has access to all public members of its super class obj.y = 20; // subclass has access to all public members of its super class obj.showA(); // subclass has access to all public members of its super class obj.z = 30; obj.showB(); } } |
Output
1 2 | x=10 y=20 x=10 y=20 z=30 |
Multi-level Inheritance
In multi level inheritance inheritance exists in multiple level.
- One Parent Class
- It has a child class
- Child has again child class
This process may continue and each child class inherits feature of all parent class.
program for Multi level inheritance in Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | class A { int x; } class B extends A { int y; } class C extends B { int z; void show() { System.out.println("x=" + x + " y=" + y + " z=" + z); /* in sub class, member of super class can be accessed directly without any object. */ } } public class Test { public static void main(String arg[]) { C objC = new C(); objC.x = 10; // subclass has access to all public members of its super class objC.y = 20; // subclass has access to all public members of its super class objC.z = 30; objC.show(); // subclass has access to all public members of its super class } } |
Result
1 | x=10 y=20 z=30 |
Hierarchical Inheritance
One Parent class can have multiple child class this type of inheritance is known as Hierarchical Inheritance.
- One Parent class
- Multiple Child class
- Each Child Inherits the feature of Parent
Write a program for Hierarchical Inheritance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | class A { int x; } class B extends A { int y; void showB() { System.out.println("x=" + x + " y=" + y); /* in sub class, member of super class can be accessed directly without any object. */ } } class C extends A { int z; void showC() { System.out.println("x=" + x + " z=" + z); /* in sub class, member of super class can be accessed directly without any object. */ } } public class Test { public static void main(String arg[]) { B objB = new B(); C objC = new C(); objB.x = 10; // subclass has access to all public members of its super class objB.y = 20; // subclass has access to all public members of its super class objC.x = 100; // subclass has access to all public members of its super class objC.z = 200; // subclass has access to all public members of its super class objB.showB(); // subclass has access to all public members of its super class objC.showC(); // subclass has access to all public members of its super class } } |
Result
1 2 | x=10 y=20 x=100 z=200 |
Hybrid Inheritance
Combination of two or more Inheritance type is known as Hybrid Inheritance
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | class A { int x; } class B extends A { int y; void show() { System.out.println("x=" + x + " y=" + y ); /* in sub class, member of super class can be accessed directly without any object. */ } } class C extends A { int z; void show() { System.out.println("x=" + x + " z=" + z); /* in sub class, member of super class can be accessed directly without any object. */ } } class D extends B { int a; void show() { System.out.println("x=" + x +" y ="+y +" a=" + a); /* in sub class, member of super class can be accessed directly without any object. */ } } public class Test3 { public static void main(String arg[]) { D d=new D(); d.x=30; d.y=20; d.a=10; d.show(); C objC = new C(); objC.x = 10; // subclass has access to all public members of its super class objC.z = 30; objC.show(); // subclass has access to all public members of its super class } } |
Note: A subclass can access the public member of
1 2 3 4 5 | javac Test3.java java Test3 x=30 y =20 a=10 x=10 z=30 |
Super Class Cant Access Sub Class Member
A subclass can access all the public members of the super class but the reverse is not true. The super class cannot access sub-class data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | class Parent // create a super class { int x; void showParent() { System.out.println("x =" + x + " y=" + y); /* error, y cant be accessed here, because y is a member of sub class. */ } } class Child extends Parent // create a sub class { int y; void showChild() { System.out.println("x =" + x + " y=" + y); } /* in sub class, member of super class can be accessed directly without any object. */ } public class TestInheritance { public static void main(String arg[]) { Child obj = new Child(); obj.x = 10; // subclass has access to all public members of its super class obj.y = 20; // subclass has access to all public members of its super class obj.showParent(); // subclass has access to all public members of its super class obj.showChild(); } } |
Result
1 2 3 4 5 6 | error: cannot find symbol System.out.println("x =" + x + " y=" + y); ^ symbol: variable y location: class Parent 1 error |
A subclass can’t access the private member of superclass.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | class A { // super class private int x; } class B extends A {// sub class int y; void showB() { System.out.println("x & y =" + x + " " + y); //error /* sub class, can’t access private member its super class . */ } } public class Test11 { public static void main(String args9[]) { B objB = new B(); objB.showB(); } } |
Result
1 2 3 4 | Test11.java:11: error: x has private access in A System.out.println("x=" + x + " y=" + y); //error ^ 1 error |