Java

Inheritance and polymorphism

Inheritance and Polymorphism in Java: Power Tools of Object-Oriented Programming

Inheritance and polymorphism are two fundamental pillars of Object-Oriented Programming (OOP) in Java. Together, they let you create flexible, reusable, and extendable code.

Let’s unpack these concepts with simple explanations and examples!


Inheritance: Building on Existing Code

Inheritance allows one class (called a child or subclass) to inherit properties and behaviors (fields and methods) from another class (called a parent or superclass).

Why use inheritance?

  • Code reuse: Don’t rewrite common features.

  • Hierarchical organization: Models “is-a” relationships (e.g., a Dog is a Animal).

  • Extensibility: Add or modify features in subclasses without changing the parent.

Syntax

java
class ParentClass {
void greet() {
System.out.println("Hello from Parent");
}
}

class ChildClass extends ParentClass {
void play() {
System.out.println("Child is playing");
}
}

Example

java
class Animal {
void eat() {
System.out.println("Animal eats food");
}
}

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

public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat(); // Inherited from Animal
myDog.bark(); // Defined in Dog
}
}


Polymorphism: Many Forms, One Interface

Polymorphism means “many forms” — a reference variable can point to objects of different classes, and the method that gets executed depends on the object’s actual type at runtime.

Polymorphism enables:

  • Method overriding: Redefine a parent class’s method in a subclass with different behavior.

  • Dynamic method dispatch: Java decides which method to call at runtime based on the object.

Example of Method Overriding

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

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

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

public class Main {
public static void main(String[] args) {
Animal myAnimal;

myAnimal = new Cat();
myAnimal.sound(); // Output: Cat meows

myAnimal = new Dog();
myAnimal.sound(); // Output: Dog barks
}
}


️ Benefits of Inheritance and Polymorphism

Feature Benefit
Inheritance Code reuse, logical hierarchy, easy updates
Polymorphism Flexible code, dynamic behavior, easier maintenance

⚠️ Important Notes

  • Use inheritance to represent “is-a” relationships.

  • Override methods carefully; use @Override annotation to avoid mistakes.

  • Polymorphism works only with reference types, not primitives.


Summary

  • Inheritance: Subclass inherits fields and methods from a superclass.

  • Polymorphism: One interface, many implementations; method calls resolved at runtime.

  • Together, they enable powerful, maintainable, and flexible Java programs.

Leave a Reply

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