UNIT 3 – Java Inheritance and Polymorphism Mechanisms Notes

Bengaluru, Tech Education Desk: Java is not just a programming language; it is a philosophy of building software that is structured, reusable, and future-ready. If Unit 2 introduced the world of classes and objects, Unit 3 elevates the journey by exploring inheritance and polymorphism—two defining pillars of object-oriented programming. Together, they provide developers with the power to extend existing code, reuse functionality, and achieve flexibility in program behavior.

Download UNIT 3 – Java Inheritance and Polymorphism Mechanisms Notes

Get simplified revision notes for this unit:
Download Unit 3 Notes PDF

Inheritance: Passing Down the Legacy

Inheritance is one of the most celebrated features of Java’s object-oriented model. It allows one class to acquire the properties and behavior of another, just as children inherit traits from their parents. The existing class is called the superclass (or parent), while the new class is known as the subclass (or child).

class Animal {
void eat() {
System.out.println("Eating...");
}
}

class Dog extends Animal {
void bark() {
System.out.println("Barking...");
}
}

Here, Dog inherits from Animal, meaning it can eat as well as bark—showing the elegance of reusability.

Types of Inheritance in Java

Unlike some languages, Java does not support multiple inheritance with classes (to avoid ambiguity), but it embraces several other forms:

  • Single Inheritance: A subclass inherits from just one parent class.

  • Multilevel Inheritance: A class inherits from another class, which in turn inherits from yet another.

  • Hierarchical Inheritance: Multiple classes inherit from the same parent class, each specializing in its own way.

Together, these models offer flexibility without compromising clarity.

The super Keyword: Reaching Back to the Parent

Sometimes, the subclass needs to explicitly call its parent’s constructor or methods. Enter the super keyword.

  • It calls the parent class constructor to ensure proper initialization.

  • It invokes the parent version of a method when the child overrides it.

class Animal {
Animal() {
System.out.println("Animal created");
}
}

class Dog extends Animal {
Dog() {
super(); // calls Animal’s constructor
System.out.println("Dog created");
}
}

With super, Java maintains clarity in the relationship between parent and child classes.

Method Overriding: Redefining Behavior

While inheritance passes down functionality, sometimes subclasses need to customize behavior. That’s where method overriding comes in. A subclass provides a new version of a method already defined in its superclass.java

class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}

The @Override annotation makes it clear that the subclass is replacing the parent’s implementation. This is essential for tailoring behavior to specific needs.

Runtime Polymorphism: Flexibility at Execution

Polymorphism, meaning “many forms,” allows one action to behave differently based on context. In Java, runtime polymorphism is achieved through dynamic method dispatch.

This occurs when a superclass reference variable points to a subclass object, and the method call is resolved at runtime—not compile-time.

Animal a = new Dog(); // superclass reference, subclass object
a.sound(); // calls Dog’s version of sound()

Here, even though the reference type is Animal, the JVM dynamically decides at runtime to execute the Dog version of sound(). This makes code adaptable and extensible.

The final Keyword: Locking the Design

Not everything in Java is meant to be extended or overridden. The final keyword provides control:

  • final variables cannot be reassigned.

  • final methods cannot be overridden.

  • final classes cannot be inherited.

For example, the String class in Java is declared as final, ensuring its immutability and preventing misuse.

final class Constants {
static final double PI = 3.14159;
}

The final keyword ensures stability in design where modification could be dangerous or unnecessary.

Abstract Classes: Laying the Blueprint

Not all classes are meant to be fully defined. Some serve as blueprints for others. Abstract classes in Java are declared with the abstract keyword and may contain abstract methods (without a body).

abstract class Shape {
abstract void draw();
}

class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
}
}

Abstract classes strike a balance between full implementation and conceptual guidance. They are the foundation upon which more specific classes are built.

Why Unit 3 is a Game-Changer

With inheritance and polymorphism, Java developers move beyond basic syntax into designing scalable, flexible systems. Code becomes reusable through inheritance, while polymorphism ensures that applications remain adaptable to future changes.

From enterprise banking applications to Android mobile systems, these features lie at the very core of how Java runs the digital world.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top