Java - Inheritance

Java

In this tutorial we will learn about inheritance in Java programming language.

Inheritance is one of the core concepts of Object Oriented Programming (OOP). It allows us to create hierarchy.

Java inheritance is like real world inheritance were a child inherits properties belonging to the parent and then the child adds some extra items on top of the inherited stuff.

Super class and Sub class

In Java using inheritance we create a parent class or superclass having common features and then we create child class or subclass that inherits features of the parent class.

For example, we can have a Person class having member variables and methods that are very general to any person like firstname, lastname etc.

Then we can have another class Employee that can have more specific member variables and methods like employeeid, payscale, joiningDate etc.

So, the Employee class can inherit the general variables and methods of the Person class and then add its own variables and methods to have a more specific class.

In programming terms the Person class will be called the superclass or the parent class. And the Employee class that inherits the Person class will be called the subclass or the child class.

The extends keyword

To create inheritance in Java we use the extends keyword.

Syntax:

class Parent {
  // some code...
}

class Child extends Parent {
  // some code...
}

Where, Parent is the name of the super class or parent class and Child is the name of the sub class or the child class.

And using the extends keyword the Child class is inheriting properties from the Parent class.

What gets inherited?

Following things get inherited by the child or subclass from the parent or superclass.

  • The public member variables of parent class.
  • The protected member variables of the parent class.
  • The public methods of the parent class.
  • The protected methods of the parent class.

The private member variables and methods of the parent class is not inherited by the child class.

How to access inherited variables and methods?

We use the . operator to access the inherited variables and methods.

Example

In the following example we have the parent or superclass Person and we have the child or subclass Employee that inherits the parent class.

// the parent class
class Person {
  // general member variables
  String firstname;
  String lastname;
}

// the child class inheriting the parent class
class Employee extends Person {
  // specific member variables
  String employeeid;
  int payscale;
  String joiningDate;

  // constructor
  Employee(String firstname, String lastname, String employeeid, int payscale, String joiningDate) {
    this.firstname = firstname;
    this.lastname = lastname;
    this.employeeid = employeeid;
    this.payscale = payscale;
    this.joiningDate = joiningDate;
  }

  // show employee details
  public void showDetail() {
    System.out.println("Employee details:");
    System.out.println("EmployeeID: " + this.employeeid);
    System.out.println("First name: " + this.firstname);
    System.out.println("Last name: " + this.lastname);
    System.out.println("Pay scale: " + this.payscale);
    System.out.println("Joining Date: " + this.joiningDate);
  }
}

// the main class
public class Example {
  
  // the main method
  public static void main(String[] args) {

    // employee data
    String firstname = "Yusuf";
    String lastname = "Shakeel";
    String employeeid = "E01";
    int payscale = 3;
    String joiningDate = "2010-01-01";

    // creating an object of the Employee class
    Employee empObj = new Employee(firstname, lastname, employeeid, payscale, joiningDate);

    // show detail
    empObj.showDetail();

  }
}

Output:

$ javac Example.java 
$ java Example
Employee details:
EmployeeID: E01
First name: Yusuf
Last name: Shakeel
Pay scale: 3
Joining Date: 2010-01-01

So, when the Employee class inherits the Person class it transforms into the following.

class Employee {
  // inherited variables from parent class
  String firstname;
  String lastname;

  // variables of the Employee class
  String employeeid;
  int payscale;
  String joiningDate;

  // constructor
  Employee(params_list) {
    // some code...
  }

  // method of the Employee class
  public void showDetail() {
    // some code...
  }
}