Java

Inheritance in Java | Explained

Object-oriented Java programming provides a concept of inheritance that makes it possible for a class to inherit the class attributes and methods of other classes. The class that inherits the characteristics of some other class is referred as a child/derived/sub-class, while the class whose characteristics are inherited is referred as a parent/base/super-class.This article presents an extensive overview of Java inheritance and in this regard, it elaborates the following concepts:

So, let’s get started!

Inheritance in Java

Inheritance is a process/mechanism that allows a class to acquire the properties of some other class, for instance, consider a father-son relationship, where a son can inherit the characteristics of his father. Let’s consider the below-given points to understand the basic concepts of inheritance in Java:

  1. In java, a class can inherit the class attributes and functions of some other class or classes.
  2. The class that inherits the properties/behavior of some other class is called the subclass while the class from whom properties/attributes are being inherited is called the superclass.
  3. A child class can be a parent of some other class for example:
  • properties of class A are inherited by class B
  • and properties of class B are inherited by class C

This means class B is a child of class A, while class C is a child of class B and grand-child of Class A.

As a class in Java can be of public, protected, and private type:

  • A public/protected member of the parent class will be accessible to the child class.
  • A private member of the parent class wouldn’t be accessible to the child class but can be accessed using get and set methods.

Syntax

In java, the extends keyword is used to inherit the properties of some other class. The basic syntax of inheritance is shown in the below-given snippet:

class​ ParentClass
{
 // class attributes of ParentClass
 // methods of ParentClass
}
class​ ChildClass ​extends​ ParentClass
{
 // class attributes of ChildClass
 // methods of ChildClass  
}

In the above snippet, the extends keyword shows that “ChildClass” is derived from “ParentClass”.

Implementation of Inheritance in Java

Let’s consider an example to understand how to inherit a class.

Example

The below-given code creates three classes:

  1. A parent class named “Person”

The Person class has class attributes such as personAge and personName and a method named display().

  1. A child class named “Employee”

The Employee class has a class attribute empId.

  1. A main class named “JavaInheritance”
package javainheritance;
class Person{
  protected int personAge = 25;
  protected String personName = "John";
  public void display()
  {
   System.out.println("This is Person class");
  }
}

class Employee extends Person{
  protected int empId = 13;
}

public class JavaInheritance {

 public static void main(String[] args) {
 Employee emp = new Employee();
 emp.display();
 System.out.println("Employee Id = " + emp.empId);
 System.out.println("Employee Name = " + emp.personName);
 System.out.println("Employee Age = " + emp.personAge);
 }    
}

In the main method we created an object of employee class named emp, and using emp we call the display() method of Person class. Moreover, personName and personAge are class attributes of Person class and inheritance makes it possible to access them with the object of the Employee class(child class).

The complete code along with its respective output is shown in the below-given figure:

The output verifies that the Employee class successfully inherits the attributes and methods of Person Class.

final Keywords in Inheritance

If we use the final keyword with a class then no other class can inherit that class. The below-given snippet will help you understand how the final keyword works:

The entire code is the same as in the previous example except for the final keyword. And the above screenshot verifies that the use of the final keyword restricts the Employee class to inherit the Person class.

Conclusion

Inheritance allows a class to access the properties of some other class and to do so, a keyword extends is used in java. After inheriting the parent class, the properties of the parent class can be accessed using the object of the child class. The final keyword can be used to prevent a class to be inherited by any other class.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.