Tutorials Hut

Java Inheritance

What is Inheritance

Inheritance means inheriting something in OOP, if a class inherits behaviour of another class it is called inheritance and class which is being inherited is called parent or super class and class which inherits parent class is called child or subclass. We look in detail on Java Inheritance in this article.

By using inheritance we can bring methods and other variables to the child class from the parent based on access level and use them as they are part of child class.

Benefits of Inheritance

  • Reusability – By inheritance we can bring parent class’ method and variables to the child class like they are part of child class and no need to write their definitions in child class.
  • Reduces Code and Effort – It helps in reducing code and effort in child classes if the parent already has desired methods and properties.
  • Maintenance – It is easy to maintain we update definition in parent and automatically new definition is available to child

Example Of Java Inheritance With Extends

In java to inherit properties of parent class we can used extend key word with child class definition and inherit property of parent class.

Syntax:
class Sup {
//Code
}

Class Sub extends Sup {
//Code
}
Java Inheritance

Java Inheritance Example

Below is an example of inheritance where Car class is parent or super class and ElectricCar is child or sub class. Child class ElectricCar has inherited startCar() and stopCar() methods from parent Car class and using it.

class Car {

public void startCar() {

     System.out.println("Car started.");

}

public void stopCar() {

     System.out.println("Car stopped.");

}

}

class ElectricCar extends Car {

public void typeOfCar() {

     System.out.println("This is electric car.");

}

}
public class InheritanceExample {

public static void main(String[] args) {

     ElectricCar electricCar = new ElectricCar();

     //inherited methods startCar() and stopCar() is available to child class ElectricCar

     electricCar.startCar();

     electricCar.stopCar();

     electricCar.typeOfCar();

}

}

Output:

Car started.
Car stopped.
This is electric car.

What is Super and Its Use

In java super is a reserved keyword is which represents parent class and its properties for example if you want to call constructor of super class in child you can add super() in child class constructor.

If you want to access methods and variables of super class which are similar or same in child, you can user super.<method/variable name> to access it.

In below example you can see in ElectricCar class acessSuperClassMethods() is accessing super class methods and variables using super keyword.

Example:

class Car {

String carCompany = "Ford";

public void startCar() {

System.out.println("Car started.");

}

public void stopCar() {

System.out.println("Car stopped.");

}

}

class ElectricCar extends Car {

public void typeOfCar() {

System.out.println("This is electric car.");

}

public void startCar() {

System.out.println("Car started in child.");

}

public void stopCar() {

System.out.println("Car stopped in child.");

}

public void acessSuperClassMethods() {

super.startCar();

super.stopCar();

System.out.println(super.carCompany);

}

}

public class InheritanceExample {

public static void main(String[] args) {

ElectricCar electricCar = new ElectricCar();

//inherited methods startCar() and stopCar() is available to child class ElectricCar

electricCar.acessSuperClassMethods();

}

}

OUTPUT:

Car started.
Car stopped.
Ford

Invoking Super class Constructor

By using super() in child class constructor with along with its arguments we can invoke super class constructor.
class Bus {
 
	public Bus(String busName) {
    	System.out.println("Super constructor, bus name is:"+busName);
	}
}
class MiniBus extends Bus {
 
	public MiniBus(String busName) {
    	super(busName);
    	System.out.println("Sub constructor, bus name is:"+busName);
	}
}
public class InheritanceExample2 {
	public static void main(String[] args) {
  	MiniBus miniBus = new MiniBus("Volvo");
	}
}

Output:

Super constructor, bus name is:Volvo
Sub constructor, bus name is:Volvo

Type Of Inheritance In Java

Below are different types of inheritance in java:
1) Single Inheritance – If a sub class inherits another super class is called single inheritance Example:  
class ClassA {
//Parent code
}
class ClassB extends ClassA {
//Child code
}
2) Multilevel Inheritance – If a sub class inherits one super class which inherits another super class and so on it is called multilevel inheritance Example:  
class ClassA {
//Parent code
}
class ClassB extends ClassA {
//Child code
}
class ClassC extends ClassB {
//Child code
}
3) Hierarchical Inheritance – If two classes inherit same parent class it will be called hierarchical inheritance Example:  
class ClassA {
//Parent code
}
class ClassB extends ClassA {
//Child code
}
class ClassC extends ClassA {
//Child code
}

4) Multiple Inheritance – If a class inheritance more than one class at same time will be called multiple inheritance, java does not support this due to diamond problem in inheritance.

Has-A and Is-A in Java

Has-A and Is-A is types of relationship between classes, we will see details on these in coming article on Is-A and Has-A in coming chapters.

References:

Refer google this article from google for reference: Inheritance 

Next Article:

Leave a Reply

Your email address will not be published. Required fields are marked *