Site icon TestingDocs.com

Java Inheritance

Overview

In this tutorial, we will learn about Java Inheritance. Inheritance is a mechanism used to develop new classes by extending the existing classes. The main advantage of inheritance is the reusability of code. In Java we used the extends keyword to extend from the existing class.

extends keyword

The keyword extends is used by the child class to inherit the features of the parent class.

public class <child_class> extends <parent_class> {

}

Example:

public class B extends A {

}

 

The existing class is known as the parent class or the superclass. In this example, A is the parent class. The inherited class is known as the subclass or the child class. B is the sub-class.

Inheritance defines an ‘is-a’ relationship between the parent class and the child class. Inheritance is a transitive relationship.

Employee is-a Person

Example:

public class Employee extends Person {

}

Java Tutorials

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/java/

Exit mobile version