Sitemap

Understanding Inheritance In Java

With Simple code examples

Press enter or click to view image in full size

What is Inheritance?

  • Inheritance is a mechanism in java that allows a class to be built on top of an existing class.
  • The new class can reuse the existing class’s methods and can add new methods and fields to adapt the new class to new situations.

Super Classes and Subclasses:

Suppose you work for a company where managers are treated differently from other employees.
Both managers and employees are paid a salary. However:

  • Employees are expected to complete the tasks in order to receive the salary.
  • Managers get bonuses if they achieve what they are supposed to do.

Since both share common properties, we can design:

  • an employee class
  • a manager class that extends Employee

👉Every manager is an employee. This is called “is-a” relationship.

Defining Subclasses:

import java.time.LocalDate;

class Employee{
private String name;
private double salary;
private LocalDate hireDate;

//Constructor
public Employee(String name, double salary, LocalDate hireDate){
this.name = name;
this.salary = salary;
this.hireDate = hireDate;
}

//getters
public String getName(){
return name;
}
public double getSalary(){
return salary;
}
public LocalDate getHireDate(){
return hireDate;
}

//setters
public void setName(String name){
this.name = name;
}
public void setSalary(double salary){
this.salary = salary;
{
public void setHireDate(double hireDate){
this.hireDate = hiredate;
}
class Manager extends Employee {
private double bonus;

public Manager(String name, double salary, LocalDate hireDate, double bonus) {
super(name, salary, hireDate);
this.bonus = bonus;
}

public double getBonus() {
return bonus;
}

// Setter for bonus
public void setBonus(double bonus) {
this.bonus = bonus;
}

@Override
public double getSalary() {
return super.getSalary() + bonus;
}
}
  • The keyword extends indicates that you are making a new class that derives from the existing class.
  • The existing class is called a superclass, base class or parent class, and the new class is called the subclass, derived class or child class.
  • A manager object can use:
    → setBonus()
    → getName() and getSalary()

Note on Private Fields
In java, private fields are not considered “inherited” because a subclass cannot access the private fields of the superclass. However, they still exist inside the subclass object. This means a manager object contains name, salary, hireDate from employee, but the manager class cannot access them directly. It must access them through protected and public methods.

Overriding Methods:

Sometimes, a method in the superclass does not fully satisfy the needs of the subclass.

Get Sameera Shaik’s stories in your inbox

Join Medium for free to get updates from this writer.

For example, a manager’s salary should include a bonus.

@Override
public double getSalary() {
return super.getSalary() + bonus;
}
/* super is a special keyword that directs the compiler to
invoke the superclass method. */

This is called method overriding, where the subclass provides its own implemenation of a superclass method.

Subclass Constructors:

public Manager(String name, double salary, LocalDate hireDate, double bonus) {
super(name, salary, hireDate);
this.bonus = bonus;
}

The super( … ) — This is the shorthand for call the constructor of the employee superclass

If a subclass constructor does not explicitly call a superclass constructor (super(…)), Java automatically calls the superclass’s no-argument constructor before executing the subclass constructor.

Inheritance Hierarchy

Inheritance need not to stop at deriving one layer of the classes.

The collection of all classes extending from a common superclass is called an Inheritance hierarchy.

Example:

class Employee {
// common fields and methods
}

class Manager extends Employee {
// extra features like bonus
}

class Developer extends Employee {
// extra features like programming language
}

class Intern extends Employee {
// limited responsibilities
}

Here, The employee is the superclass, and Manager, developer, Intern are the subclasses. All of them shares the common properties like name and salary, but each subclass can have its own additional features.

Types of Inheritance

Inheritance can be organized in different ways based on class relationships.

Single Inheritance
Here the subclass will be derived from one superclass

class Employee{
// common properties
}
class Manager extends Employee{
// additional features
}

Mutlilevel Inheritance
A class inherits from another subclass, forming a chain.

class Employee{}

class manager extends Employee{
}

class seniorManager extends manager{
}

Hierarchical Inheritance
Mutlitle classes inherits from a single superclass.

class Employee {
// common fields and methods
}

class Manager extends Employee {
// extra features like bonus
}

class Developer extends Employee {
// extra features like programming language
}

class Intern extends Employee {
// limited responsibilities
}

Multiple Inheritance
A class inherits from more than one class.

interface A {
}

interface B {
}

class C implements A, B {
}

In java, Multiple Inheritance is not supported with classes. However, it can achieve using interfaces.

Conclusion

Inheritance allows us to reuse and extend existing code, making programs more organized and maintainable. By using concepts like method overriding, constructors, and the super keyword, we can build flexible and scalable applications.

0 followers

Learning and writing along the way.