Java inheritance with Example
Java inheritance Java inheritance is a mechanism in which a child object acquires all the properties and behaviors of a parent object. It helps in reusing the code and establishes a relationship between different classes. It is an essential part of oops (object-oriented programming system). As we know in real life, a son inherits the properties from his father just like that inheritance acts in Java. Java follows the similar concepts of son and father relationship. Java inheritance represents a parent-child relationship which is also known as IS-A relationship. Java has two classes
- Parent class / Superclass / Base class
- Child class/ Subclass /Derived class
class Teacher{ String prof = "Teacher"; String col_n = "IIT K"; void does(){ System.out.println("Teaching"); } } public class CSTeacher extends Teacher{ String sub = "Java"; public static void main(String args[]){ CSTeacher z = new CSTeacher(); System.out.println(z.col_n); System.out.println(z.prof); System.out.println(z.sub); z.does(); } }Output
IIT K Teacher Java TeachingIn the above example, the Child class CSTeacher holds the attributes and methods of parent class Teacher like designation, collegeName and void does(). The extends keyword indicates that you are using properties in a new class that derives from an existing class.
Advantage of Inheritance
- Inheritance helps to achieve runtime polymorphism for method overloading.
- Inheritance facilitates with code reusability.
- Inheritance decreases the line of codes in Java program.
- Inheritance minimizes the amount of duplicate code by sharing the standard code amongst several subclasses.
- Inheritance facilitates with data hiding feature. The base class can decide to keep some data private so that the derived class can not alter it.
Disadvantage of Inheritance
The main disadvantage of inheritance is that two classes (base and derived) are tightly coupled that it cannot be used independently of each other. Another disadvantage of the inheritance is that it increases time/ effort it takes the program through all levels of overloaded class.Types of inheritance in Java
Different types of inheritance supported by Java are as follows.- Single Inheritance
- Multilevel Inheritance
- Hierarchical inheritance
Single inheritance
In single inheritance, one class inherits only another class.
public class A { public void print1() { System.out.println("Class A method"); } } class B extends A { public void print2() { System.out.println("Class B method"); } public static void main(String[] args) { B d = new B(); d.print1(); d.print2(); d.print1(); } }Output
Class A method Class B method Class A method
Multilevel inheritance
In multilevel inheritance, a child class will be inheriting a parent class as well as the child class, and also parent class for some other class. In the given diagram, class A serves as a parent class for the class B., and also class B serves as a parent class for class C. This process is known as multilevel inheritance. A class can’t access directly inherit the grandparent class.
public class A { public void print1() { System.out.println("Class A method"); } } class B extends A { public void print2() { System.out.println("Class B method"); } } class C extends B { public void print3() { System.out.println("Class C method"); } public static void main(String[] args) { C d = new C(); d.print1(); d.print2(); d.print3(); }}Output
Class A method Class B method Class C method
Hierarchical inheritance
In hierarchical inheritance, a single base class serves many sub classes. More than one child classes can inherit the properties of the parent class.
java hierarchical inheritance Example
class A { public void print1() { System.out.println("Class A method"); } } class B extends A { public void print2() { System.out.println("Class B Method"); } } class C extends A { public void print3() { System.out.println("Class C method"); } } public class Main { public static void main(String[] args) { C d = new C(); d.print1(); B b = new B(); b.print2(); d.print3(); } }Output
Class A method Class B Method Class C method
Super keyword in Java inheritance
In Java, the keyword ‘super’ and ‘this’ are very similar. Super keyword refers to the object of the base (parent) class. It acts as a reference variable. Super can be used in the following context.- Use of super with variables
- Use of super with methods
- Use of super with constructors
Some scenarios with the super keyword
Let’s see an example to understand the super keyword. Example 1class ParentClass { int n = 50; } class ChildClass extends ParentClass { int n = 80; void printNumber(){ System.out.println(n); } public static void main(String args[]){ ChildClass obj= new ChildClass(); obj.printNumber(); } }Output
80In the given example, we have avoided the use of the super keyword. Now see the example with super keyword.
class ParentClass { int n = 50; } class ChildClass extends ParentClass { int n = 80; void printNumber(){ System.out.println(super.n); } public static void main(String args[]){ ChildClass obj= new ChildClass(); obj.printNumber(); } }Output
50In the given example, when we use the super keyword then it refers the parent class object. Example 2 Let’s understand how super keyword acts with parent class constructor.
class ParentClass { ParentClass() { System.out.println("Parent class Constructor"); } } class ChildClass extends ParentClass { ChildClass() { super(); //invoke super class constructor System.out.println("Child class Constructor"); } } class SuperDemo { public static void main(String[] args) { ChildClass c = new ChildClass(); } }Output
Parent class Constructor Child class Constructor This is an example of use of super keyword with constructor.
Question: Does Java supports multiple inheritance, explain?
Answer Java doesn't support multiple inheritance through classes, but It can be achieved through interfaces. It has some conflict like the diamond problem that is why java developers excluded this feature from java.
Question: Explain diamond problem in Java.
Answer: The diamond problem The following diagram and program will clear the image of the diamond problem.
class A { void m1() { System.out.println("class A"); }} class B extends A { void m1() { System.out.println("Class B"); } } class C extends A { void m1() { System.out.println("Class C"); } } class Test extends A, B { public static void main(String args[]) { Test d1 = new Test(); d1.m1(); } }Output-
This program will produce compile time error.As we can see in the above program on calling method m1() using D class object will cause complication. The program got confused whether it calls A's method m1() or B's method m1() because class D inheriting both class A and B. This complicated situation is called diamond problem.