Java - Inheritance - Accessing inherited variables and methods

Java

Share

In this tutorial we will learn how to use inherited variables and methods in Java programming language.

In the previous tutorial Java - Inheritance we learned about inheritance. Feel free to check that out.

So, we talked about the parent class Person and child class Employee. Let us now enhance that example and add some methods to the parent class and use it in the child class.

Example

In the following example we are adding methods to the parent class Person which will be inherited by the child class Employee.

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

  // general methods
  public void setFirstName(String firstname) {
    this.firstname = firstname;
  }

  public void setLastName(String lastname) {
    this.lastname = 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) {
    
    // set the firstname and lastname using the
    // setFirstName() and setLastName() methods
    // of the parent class Person
    // that is inherited by this child class Employee
    this.setFirstName(firstname);
    this.setLastName(lastname);

    // now set the member variables of this class
    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;

  // inherited methods from parent class
  public void setFirstName(String firstname) {
    this.firstname = firstname;
  }

  public void setLastName(String lastname) {
    this.lastname = 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...
  }
}
Share