In this article we will see 'What is Inheritance in Java', How to used it, super class and sub classes with example.

What is Inheritance ?

When one object acquires the properties of another object this is called inheritance. The inheriting class is called sub class and the class which is being inherited is called super class. In Java inheritance is used to code re usability where Code written in super class can be used in sub class.

Inheritance also promotes polymorphism by allowing method overriding.

A class inherits another class using "extends" keyword, every class in Java by default extends java.util.Object, apart from this a class can extends exactly one other class only.
// Super Class
class Human {
	public void printName(String name) {
		System.out.println(name);
	}
}
//Sub Class
class Male extends Human{

}
//Sub Class
class Female extends Human{

}
In the example above 'Human' super class is extended by two sub classes 'Male' and 'Female'. This way printName(String name) written in 'Human' class can be called by instances of 'Male' and 'Female' classes to achieve code re usability.
Male male = new Male();
male.printName("John");
		
Female female = new Female();
female.printName("Anaida");
Case 1: When super class and subclass both have same methods

When superclass and subclass both have same methods, this phenomena is called method overriding:
// Super Class
class Human {
	public void printName() {
		System.out.println("Human");
	}
}
//Sub Class
class Male extends Human{
	public void printName() {
		System.out.println("Male");
	}
}
While calling printName() with Subclass Object and Reference Variable or Subclass object but Super class Reference variable, method in subclass will be called:
public class Main{
	public static void main(String args[]) {
		Male male = new Male();
		male.printName(); // prints 'Male'
		
		Human human = new Male();
		human.printName(); // prints 'Male'
	}
}
Case 2: When superclass has a method but subclass dont have same method:

When superclass has a method but subclass dont have same method, the method in superclass can be called with subclass instance:
// Super Class
class Human {
	public void printName() {
		System.out.println("Human");
	}
}
//Sub Class
class Male extends Human{
}
While calling printName() with Subclass Object and Reference Variable or Subclass object but Super class Reference variable, method in Super class will be called:
public class Main{
	public static void main(String args[]) {
		Male male = new Male();
		male.printName(); // prints 'Human'
		
		Human human = new Male();
		human.printName(); // prints 'Human'
	}
}

Multilevel Inheritance

When A class extends B and C extends B and so on, this is called Multilevel inheritance, as shown below:
class A {
public void printName() {
		System.out.println("A");
	}
}

class B extends A{
}

class C extends B{
}
In the case of Multilevel Inheritance any subclass in the hierarchy can call any superclass methods:
public class Main {
	public static void main(String args[]) {
		C c = new C();
		c.printName(); // prints 'A'
	}
}
Multiple inheritance means a class extending more than one class. Java does not support multiple inheritance because in that case an ambiguity will occur, if more than one super class contains same method with different implementation and subclass try to call that method, compiler will not be able to recognise the super class from which method has to be called.

In this article we have seen 'What is Inheritance in Java', How to used it, super class and sub classes with example. In upcoming articles we will see more about Java.
  • By Techburps.com
  • Nov 7, 2017
  • Core Java