Inheritance and Polymorphism


Introduction
In the previous lesson, we explored Object-Oriented Programming (OOP) Concepts, covering Encapsulation, Inheritance, Polymorphism, and Abstraction. Now, we focus on two fundamental OOP principles: Inheritance and Polymorphism, which enable code reusability and flexibility in Java.
What is Inheritance?
Inheritance allows a class to acquire the properties and behaviors of another class, promoting code reuse and hierarchical relationships between classes.
When to Use Inheritance?
When multiple classes share common attributes and methods.
When creating a general-to-specific class hierarchy (e.g.,
Vehicle → Car
).
Example: Extending a Class
abstract class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
class Bird extends Animal {
void chirp() {
System.out.println("Bird chirps");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.makeSound(); // Inherited method
myDog.bark(); // Own method
}
}
/**
* Output:
* Animal makes a sound
* Dog barks
*/
Method Overriding
Method Overriding occurs when a subclass provides a specific implementation of a method already defined in its parent class.
When to Use Method Overriding?
- When a subclass needs a different behavior for an inherited method.
Example:
class Parent {
void show() {
System.out.println("Parent class method");
}
}
class Child extends Parent {
@Override
void show() {
System.out.println("Child class method");
}
}
public class Main {
public static void main(String[] args) {
Parent obj = new Child(); // Upcasting
obj.show(); // Calls overridden method in Child
}
}
/**
* Output:
* Child class method
*/
What is Polymorphism?
Polymorphism allows a method to perform different behaviors based on the object it belongs to. It enhances flexibility and scalability.
When to Use Polymorphism?
When different classes need to provide different implementations for the same method.
When implementing interfaces and abstract classes.
Example: Compile-Time Polymorphism (Method Overloading)
class MathUtils {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
public class Main {
public static void main(String[] args) {
MathUtils obj = new MathUtils();
System.out.printf("Answer: %d%n", obj.add(5, 10)); // Calls first method
System.out.println("Answer: %d%n", obj.add(5, 10, 15)); // Calls second method
}
}
/**
* Output:
* Answer: 15
* Answer: 30
*/
Example: Runtime Polymorphism (Method Overriding)
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Cat extends Animal {
@Override
void makeSound() {
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Cat(); // Upcasting
myAnimal.makeSound(); // Calls overridden method in Cat
}
}
/**
* Output:
* Cat meows
*/
Conclusion
Inheritance and Polymorphism are essential OOP principles that enhance code reusability and flexibility. Inheritance allows code sharing across related classes, while Polymorphism ensures objects can behave differently based on context. Understanding method overriding, method overloading, interfaces, and abstract classes is key to mastering Java’s OOP paradigm. Keep practicing, and happy coding!
Subscribe to my newsletter
Read articles from Emmanuel Enchill directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Emmanuel Enchill
Emmanuel Enchill
I am a passionate software engineer in training at ALX. I have a strong preference for back-end development. I am on course to share my learning journey with you.