DEV Community

Oleksandr
Oleksandr

Posted on

Java: Inheritance, Method Overrides, and Polymorphism.

Hey everyone!

In my previous post, I covered encapsulation, getters and setters, access modifiers. Now let's move on to the following fundamental concepts of object-oriented programming: inheritance, method overrides, and polymorphism.

Inheritance

Definitions: A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).

The idea of inheritance is simple but powerful: When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class.

A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

Example of Inheritance:

// Super class
class Animal {
    String name;

    public Animal(String name) {
        this.name = name;
    }

    public void eat() {
        System.out.println(name + " is eating.");
    }

    public void sleep() {
        System.out.println(name + " is sleeping.");
    }
}

// Subclass
class Dog extends Animal {

    public Dog(String name) {
        super(name); // Calling super class constructor
    }

    public void bark() {
        System.out.println(name + " is barking.");
    }
}

// Entry point
public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog("Rex");

        myDog.eat();     // Inherited from Animal
        myDog.sleep();   // Inherited from Animal
        myDog.bark();    // Method of the Dog class
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Rex is eating.
Rex is sleeping.
Rex is barking.
Enter fullscreen mode Exit fullscreen mode

Key Facts About Inheritance in Java

  • Inheritance creates an “is-a” relationship If Dog extends Animal, then a Dog is an Animal
  • The extends keyword is used Use extends to create a subclass:

class Dog extends Animal { ... }

  • A subclass inherits everything except private members
  • Use super to access parent methods or constructors super() calls the parent constructor. super.methodName() calls a parent class method.
  • In Java a class can extend only one other class.
  • Avoid deep inheritance hierarchies Too many levels of inheritance can make code harder to understand and maintain.

Method Overriding

What is Method Overriding?

Method overriding occurs when a subclass provides its own implementation of a method that is already defined in its superclass.

This allows a subclass to define specific behavior while still using the general structure of the superclass.

To override a method:

  • The method must exist in the superclass.
  • The method in the subclass must have the same name, return type, and parameters.
  • Use the @Override annotation (optional but recommended for clarity and compiler checks).

Example of Method Overriding

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

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

public class Main {
    public static void main(String[] args) {
        Animal myAnimal = new Dog(); // Polymorphism in action
        myAnimal.sound(); // Calls overridden method in Dog
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Dog barks
Enter fullscreen mode Exit fullscreen mode

Key Points About Method Overriding

  • The method must have the same signature as the one in the superclass.
  • The overridden method can have a more specific access modifier (e.g., protectedpublic).
  • You can use super.methodName() to call the superclass version of the method from the subclass.
  • Overriding enables runtime polymorphism.

When Should You Override?

  • When a subclass needs to customize or specialize a method's behavior. Example: An Animal class with a sound() method, overridden by Dog, Cat, Cow, etc.

Polymorphism

What is Polymorphism?

Polymorphism means “many forms”. In Java, it allows one interface to be used for a general class of actions.

With polymorphism, a parent class reference can point to a child class object, and the correct method is chosen at runtime.

This is a powerful concept because it allows you to write flexible and reusable code.

Example of Polymorphism in Java:

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

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

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

public class Main {
    public static void main(String[] args) {
        Animal myAnimal1 = new Dog();
        Animal myAnimal2 = new Cat();

        myAnimal1.makeSound(); // Dog barks
        myAnimal2.makeSound(); // Cat meows
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Dog barks  
Cat meows

Enter fullscreen mode Exit fullscreen mode

Key Points About Polymorphism

  • Polymorphism allows methods to behave differently based on the object that’s calling them.
  • It works with method overriding — the subclass method overrides the parent version.
  • Enables code generalization: one method can accept many types of objects.
  • Achieved through inheritance + method overriding.
  • Only methods are polymorphic — fields are not.

Why Use Polymorphism?

  • Simplifies code and reduces duplication

  • Makes your code more extensible and maintainable

  • Encourages the use of interfaces and abstract classes

Whats next

In the upcoming posts, we’ll explore more advanced and practical concepts in Java:

  • Abstract Classes & Interfaces
  • Exception Handling
  • Enums and Constants

To reinforce what you’ve learned in this post:

  • Create your own class hierarchy. For example, AnimalBird, Fish, and Reptile.
  • Override a method like makeSound() in each subclass with different output.
  • Try calling overridden methods from the main class using different subclass objects.

This will help you better understand inheritance, method overriding, and polymorphism in action.

Top comments (0)