Chapter 3: Inheritance, Polymorphism & Abstraction


Introduction

As enterprise applications grow, developers often encounter duplicate code across multiple classes.

Consider the IPMS application.

Entities such as:

  • Student
  • Company
  • Placement Drive
  • Interview Panel Member
  • Administrator

share several common characteristics.

Instead of duplicating code, Object-Oriented Programming provides powerful mechanisms for code reuse and extensibility.

In this chapter, we will learn:

  • Inheritance
  • Polymorphism
  • Abstraction

These concepts are fundamental for designing scalable enterprise applications.


What You Will Learn

By the end of this chapter, you will:

  • Understand Inheritance
  • Create Parent and Child Classes
  • Use Method Overriding
  • Understand IS-A Relationships
  • Learn Runtime Polymorphism
  • Learn Compile-Time Polymorphism
  • Create Abstract Classes
  • Implement Abstract Methods
  • Apply Inheritance in IPMS
  • Build Reusable Class Hierarchies

Understanding Inheritance

Inheritance allows one class to acquire the properties and behaviors of another class.


Benefits of Inheritance

  • Code Reuse
  • Maintainability
  • Extensibility
  • Reduced Duplication
  • Better Design

Real World Example

Consider:

Person
│
├── Student
│
├── Administrator
│
└── Recruiter

All entities share:

  • Name
  • Email
  • Phone Number

Parent Class

public class Person {

    protected String name;

    protected String email;

    protected String phone;
}

Child Class

public class Student extends Person {

    private double cgpa;
}

Understanding extends

extends

creates an inheritance relationship.


Inheritance Hierarchy

Inheritance Hierarchy


Creating Parent Class

package com.ipms.person;

public class Person {

    protected Long id;

    protected String name;

    protected String email;
}

Creating Student Class

package com.ipms.student;

import com.ipms.person.Person;

public class Student extends Person {

    private double cgpa;
}

Creating Recruiter Class

package com.ipms.recruiter;

import com.ipms.person.Person;

public class Recruiter extends Person {

    private String designation;
}

Accessing Parent Members

Student student = new Student();

student.name = "John Doe";

Understanding IS-A Relationship

Inheritance represents:

IS-A

Examples:

Student IS-A Person

Recruiter IS-A Person

Admin IS-A Person

Example

public class Employee {

}
public class Manager extends Employee {

}

Invalid Example

Company IS-A Student

Invalid relationship.


Constructor Inheritance

Parent constructors execute before child constructors.


Parent Constructor

public Person() {

    System.out.println("Person Constructor");
}

Child Constructor

public Student() {

    System.out.println("Student Constructor");
}

Output

Person Constructor

Student Constructor

Understanding super Keyword

Used to access parent members.


Calling Parent Constructor

public Student() {

    super();
}

Parent Parameterized Constructor

public Person(
        String name,
        String email) {

    this.name = name;

    this.email = email;
}

Child Constructor

public Student(
        String name,
        String email,
        double cgpa) {

    super(name, email);

    this.cgpa = cgpa;
}

Accessing Parent Method

super.displayInfo();

Method Overriding

A child class can redefine parent behavior.


Parent Method

public void displayRole() {

    System.out.println("Person");
}

Child Method

@Override
public void displayRole() {

    System.out.println("Student");
}

Example

public class Person {

    public void displayRole() {

        System.out.println("Person");
    }
}

public class Student extends Person {

    @Override
    public void displayRole() {

        System.out.println("Student");
    }
}

Executing

Student student = new Student();

student.displayRole();

Output

Student

Rules for Overriding

  • Same method name
  • Same parameters
  • Same return type
  • Cannot reduce visibility

Understanding Polymorphism

Polymorphism means:

Many Forms

Types of Polymorphism

  1. Compile-Time Polymorphism
  2. Runtime Polymorphism

Compile-Time Polymorphism

Achieved using:

Method Overloading

Example

public class Calculator {

    public int add(
            int a,
            int b) {

        return a + b;
    }

    public int add(
            int a,
            int b,
            int c) {

        return a + b + c;
    }
}

Using Overloaded Methods

Calculator calculator =
        new Calculator();

calculator.add(10, 20);

calculator.add(10, 20, 30);

Runtime Polymorphism

Achieved using:

Method Overriding

Example

Person person =
        new Student();

Dynamic Method Dispatch

person.displayRole();

calls:

Student.displayRole()

Polymorphism Overview

Polymorphism Overview


Example

public class Person {

    public void login() {

        System.out.println("Person Login");
    }
}

public class Student extends Person {

    @Override
    public void login() {

        System.out.println("Student Login");
    }
}

public class Recruiter extends Person {

    @Override
    public void login() {

        System.out.println("Recruiter Login");
    }
}

Runtime Execution

Person person;

person = new Student();

person.login();

person = new Recruiter();

person.login();

Output

Student Login

Recruiter Login

Understanding Abstraction

Abstraction focuses on:

What an object does

instead of

How it does it

Benefits

  • Simplifies Design
  • Improves Maintainability
  • Supports Extensibility
  • Reduces Complexity

Abstract Classes

An abstract class cannot be instantiated.


Example

public abstract class Person {

}

Invalid

Person person =
        new Person();

Abstract Method

public abstract void login();

Abstract Class Example

public abstract class Person {

    protected String name;

    public abstract void login();
}

Student Implementation

public class Student extends Person {

    @Override
    public void login() {

        System.out.println(
                "Student Login");
    }
}

Recruiter Implementation

public class Recruiter extends Person {

    @Override
    public void login() {

        System.out.println(
                "Recruiter Login");
    }
}

Using Abstract Classes

Person person =
        new Student();

person.login();

Output

Student Login

Abstract Class with Concrete Methods

public abstract class Person {

    public void displayInfo() {

        System.out.println("Person");
    }

    public abstract void login();
}

Enterprise Example

Person
│
├── Student
│
├── Recruiter
│
└── Administrator

IPMS User Hierarchy

IPMS User Hierarchy


Creating Base User

public abstract class User {

    protected Long userId;

    protected String name;

    protected String email;

    public abstract void login();
}

Student User

public class Student
        extends User {

    private double cgpa;

    @Override
    public void login() {

        System.out.println(
                "Student Login");
    }
}

Recruiter User

public class Recruiter
        extends User {

    private String company;

    @Override
    public void login() {

        System.out.println(
                "Recruiter Login");
    }
}

Administrator User

public class Administrator
        extends User {

    @Override
    public void login() {

        System.out.println(
                "Admin Login");
    }
}

Testing Polymorphism

User user;

user = new Student();

user.login();

user = new Recruiter();

user.login();

user = new Administrator();

user.login();

Output

Student Login

Recruiter Login

Admin Login

Mini Project

Create User Management Module.

Requirements:

  • Create abstract User class
  • Create Student class
  • Create Recruiter class
  • Create Administrator class
  • Override login()
  • Demonstrate runtime polymorphism

Best Practices

  • Favor inheritance for IS-A relationships
  • Keep inheritance hierarchies shallow
  • Use @Override annotation
  • Use abstraction for common behavior
  • Avoid duplicate code
  • Follow single responsibility principle

Common Beginner Mistakes

Mistake Correct Approach
Deep inheritance chains Keep hierarchy simple
Forgetting @Override Always use annotation
Instantiating abstract class Create child object
Wrong IS-A relationship Use proper hierarchy
Duplicate methods Use inheritance

Practice Exercises

  1. Create Employee and Manager classes.
  2. Create Vehicle hierarchy.
  3. Implement method overriding.
  4. Create abstract Payment class.
  5. Implement CreditCardPayment.
  6. Implement UPIPayment.
  7. Demonstrate runtime polymorphism.

Chapter Summary

In this chapter, you learned:

  • Inheritance
  • Parent and Child Classes
  • IS-A Relationships
  • Constructor Chaining
  • super Keyword
  • Method Overriding
  • Compile-Time Polymorphism
  • Runtime Polymorphism
  • Abstract Classes
  • Abstract Methods
  • Enterprise Class Hierarchies

In the next chapter, we will learn:

  • Interfaces
  • Collections Framework
  • Generics

These concepts form the foundation of enterprise Java application development.

Need help with your project?
I can help you explore our services or start an inquiry.