Inheritance in Java or Is a relationship in Java : Tutorial and Interview Question

Inheritance is one of OOPS concept in Java where child class inherits the method of parent class. It emphasize on the res-usability of the code.

some facts about Inheritance

1) Inheritance is implements by extends keyword in java.
2) Inheritance make the re-usability of code ie child class can access the methods of parent class via inheritance.


Different types of Inheritance 

1) Single-level inheritance

one class inherits other example

Class Parent{
}
Class Child extends Parent{
}

2) Multilevel Inheritance 
example –

Class Grand_Father{
}
Class Father extends Grand_Father{
}
Class Child extends Father}
}

3) Hierarchical Inheritance
example –
Class Father {
}
Class son extends Father{
}
Class Daughter extend Father {
}

4) Multiple inheritance –  It is not supported in Java

Class father {
}
Class Mother{
}
Class son extends father , mother  {
// java does not support
}


Java code to demonstrate child class and parent class reference concept

This is one of the important concept and many time interviewer can ask this question from parent child object and reference concept.

Case 1 :  parent class object and parent class reference

// inheritance in java
class parent
{
	public void parent_method()
	{
		System.out.println("In Parent Class");
	}
}

public class child extends parent{

	public void child_method()
	{
		System.out.println("In Child Class");
	}

	public static void main(String[] args) {
		
		// parent object and parent reference
		parent p1 = new parent();
		p1.parent_method(); // output -> In Parent Class
		// parent cannot call child class method
		p1.child_method(); // --> compile time error

		
		}
}

Note : Parent class object cannot access method of child class.


Case 2 : child class object and child class reference

// inheritance in java
class parent
{
	public void parent_method()
	{
		System.out.println("In Parent Class");
	}
}

public class child extends parent{

	public void child_method()
	{
		System.out.println("In Child Class");
	}

	public static void main(String[] args) {
		
		// child object and child reference
		child c1 = new child();
		c1.parent_method(); // Output -> In Parent Class
		c1.child_method(); // Output -> In Child Class


		
		}
}

Note : Child reference and child object can call all methods of child and parent class.


Case 3 : parent reference and child class object

// inheritance in java
class parent
{
	public void parent_method()
	{
		System.out.println("In Parent Class");
	}
}

public class child extends parent{

	public void child_method()
	{
		System.out.println("In Child Class");
	}

	public static void main(String[] args) {
		
		// parent reference and child object
		parent p1 = new child();
		p1.parent_method(); // output -> In Parent Class	
		// parent object cannot call child method
		p1.child_method(); // compile time error
		}
}

 

Note : Parent reference can hold child object but cannot call child class method.


case 4 : child reference and parent class object

// inheritance in java
class parent
{
	public void parent_method()
	{
		System.out.println("In Parent Class");
	}
}

public class child extends parent{

	public void child_method()
	{
		System.out.println("In Child Class");
	}

	public static void main(String[] args) {
		
		// child object and parent reference
		// child class reference cannot be used to access parent class object
		child c1 = new parent();
		// compile time error 
		}
}

It will give compile time error.

Note : child class reference cannot be used to access parent class object.


lets have an example to demonstrate the advantage of inheritance

Suppose we have 3 classes and all three classes have 200 common methods and 100 specific methods of each class.

// child class 1

class child1 {
// 200 common methods
// 100 specific methods
// total 300 methods need to define
}

// child class 2
class child2
{
// 200 common methods
// 100 specific methods
// total 300 methods need to define
}

// child class 3
class child3
{
// 200 common methods
// 100 specific methods
// total 300 methods need to define
}

hence total method defined in all classes is 300 + 300 + 300 = 900 methods as common methods are defined in every child class.


Now implement inheritance concept here and see

declare one parent class and define all 200 common methods in that and let other child classes inherit that

// parent class
class parent{
// 200 common methods
}

// child class 1
class child1 extend parent
{
// 200 common methods inherited
// 100 specific methods
}

// child class 2
class child2 extend parent
{
// 200 common methods inherited
// 100 specific methods
}

// child class 3
class child3 extend parent{
// 200 common methods inherited
// 100 specific methods
}

hence total method define is 200 + 100 + 100 + 100 = 500 methods only

common methods are defined only once in parent class and child class re-use the methods and save the time.


There are several parent class in Java like object class is parent class of all classes in java like string class , string buffer , throwable etc. Object class has 11 methods and it is inherited by all it child classes.

Object class has the most common methods which are inherited by all java classes. Hence Object class is the root class for all the classes in Java.

Now in your mind there might be a question that if object class is inherited by all classes by default in java so how any class inherit any other class as multiple inheritance is not allowed in java

Note : Classes in Java can extend only one class, hence Multiple inheritance is not allowed in Java by classes.

example – class child extends parent1,parent2 is not allowed

Important concept 

Case 1 : class is not inherit any class then by default it extends object class.

java-inheritance

Case 2 : In Java if any class extends any other class then it is indirect child of object class not the direct child as multiple inheritance is not allowed in java.

java-inheritance

Note : Java does not support multiple inheritance directly or indirectly with respect to classes.


Most interesting Java interview question

Why Java class does not support multiple inheritance ?

Answer : Suppose there are two parent classes parent1 and parent2 and both have same method parent_method(). Now if any child class extends both parent class and call method parent_method() then it will create ambiguity that which method to called by child class. Hence Java does not support multiple inheritance.

suppose child class extends two parents class

Class parent1{
parent_method();
}

Class parent2{
parent_method();
}

Class child extends parent1,parent2
{
child c = new child();
c.parent_method // which parent method it will call and creates ambiguity
// both parent methods have same name so JVM is confused to call which method
}

Hence Java class does not support multiple inheritance.


Note : Multiple inheritance is achieved by Interfaces in Java . Interface can extend any number of interfaces.

Interface parent1
{
}

Interface parent2
{
}

Interface child extends parent1,parent2
{
}

Note : ambiguity is not an issue in interfaces as implementation is done at class level and interface just declare the methods.

Interface parent1
{
method1();
}

Interface parent2
{
method1();
}

Interface child extends parent1,parent2
{
method1();
}

class A implements child
{
// method1() defined here
// hence no ambiguity
}


This is all about the inheritance in Java and now with this tutorial you are all set for Java interview questions of inheritance. Hope you like this article.