DEV Community

Ajay Sundar
Ajay Sundar

Posted on

Like Father Like Son : A Begginer's Guide to Java Inheritance

Inheritance is one of the core pillars of Object-Oriented Programming (OOP). It allows one class (child/subclass) to acquire the properties and behaviors (fields and methods) of another class (parent/superclass).

Single Inheritance

A subclass inherits from one superclass

Multilevel Inheritance

A class is derived from another class, which is also derived from another class.

Hierarchical Inheritance
Multiple classes inherit from the same parent class

Multiple Inheritance

Java doesn’t allow multiple inheritance with classes (to avoid conflicts).
But it supports multiple inheritance with interfaces.

Hybrid Inheritance

Hybrid inheritance is a combination of two or more types of inheritance.
Java does not support hybrid inheritance with classes, but it can be achieved using interfaces.

Real world example using inheritance
In Object-Oriented Programming (OOP), inheritance is one of the most powerful features. It allows a class (child) to acquire the properties and methods of another class (parent). This helps in code reusability and makes the program more structured.

To understand this better, let’s look at a real-world example — a training institute called Payilagam.

class Institute {
    void Morning() {
        System.out.println("Morning session starts at 9:00AM");
    }
    void Afternoon() {
        System.out.println("Afternoon session starts at 2:00PM");
    }
    void Communication() {
        System.out.println("Communication starts at 3:30PM");
    }
    void Aptitude() {
        System.out.println("Aptitude starts at 4:30");
    }
}

Enter fullscreen mode Exit fullscreen mode

Here, the Institute class is the parent (superclass).
It defines common sessions like Morning, Afternoon, Communication, and Aptitude.

The Derived Classes – Students, Teachers, Facilities

  1. Students Class
class Students extends Institute {
    void MorningSession() {
        System.out.println("Morning Session is always productive");
    }
    void Lunch() {
        System.out.println("Lunch is a time where we get time to interact");
    }
    void EveningSession() {
        System.out.println("Evening session feels tired because of after lunch");
    }
}

Enter fullscreen mode Exit fullscreen mode
  • Students inherit all sessions from Institute.

  • They also describe their own experiences like MorningSession, Lunch, and EveningSession.

  1. Teachers Class
class Teachers extends Institute {
    void MuthuSir() {
        System.out.println("Muthu sir is a great speaker when it comes about life");
    }
    void PrithviSir() {
        System.out.println("Prithvi sir is our Java Trainer");
    }
    void SalmanSir() {
        System.out.println("Salman sir Communication class is very interactive");
    }
}

Enter fullscreen mode Exit fullscreen mode
  • Teachers too inherit sessions from Institute.

  • They add specific methods that describe different trainers.

3.Facilities Class

class Facilities extends Institute {
    void Airconditioner() {
        System.out.println("The AirConditioner is great at Payilagam");
    }
    void InternetAccess() {
        System.out.println("The internet access is medium at Payilagam");
    }
    void Waterfacilities() {
        System.out.println("The water facilities is great at payilagam");
    }
}


Enter fullscreen mode Exit fullscreen mode
  • Facilities also extend Institute.

  • This class focuses on infrastructure features like air conditioning, internet, and water.

The Driver Class – Payilagam

public class Payilagam {
    public static void main(String[] args) {
        Institute obj = new Institute();
        Teachers obj1 = new Teachers();
        Students obj2 = new Students();
        Facilities obj3 = new Facilities();

        obj.Morning();
        obj.Afternoon();
        obj.Communication();
        obj.Aptitude();

        obj2.MorningSession();
        obj2.Lunch();
        obj2.EveningSession();

        obj1.MuthuSir();
        obj1.PrithviSir();
        obj1.SalmanSir();

        obj3.Airconditioner();
        obj3.InternetAccess();
        obj3.Waterfacilities();
    }
}

Enter fullscreen mode Exit fullscreen mode

Here, we:

  • Created objects of Institute, Teachers, Students, and Facilities.

  • Called methods to display their respective messages.

OUTPUT

Morning session starts at 9:00AM
Afternoon session starts at 2:00PM
Communication starts at 3:30PM
Aptitude starts at 4:30
Morning Session is always productive
Lunch is a time where we get time to interact
Evening session feels tired because of after lunch
Muthu sir is a great speaker when it comes about life
Prithvi sir is our Java Trainer
Salman sir Communication class is very interactive
The AirConditioner is great at payilagam
The internet access is medium at Payilagam
The water facilities is great at payilagam
Enter fullscreen mode Exit fullscreen mode

Key Takeaways

  • Inheritance in Action: All classes extend Institute, reusing its session methods.

  • Code Reusability: Instead of redefining sessions for each class, they are inherited.

  • Real-World Mapping: Students, Teachers, and Facilities represent real entities in an institute.

  • Scalability: We can easily add new classes like Events or Exams in the future without changing the existing code.

Top comments (0)