codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Subjects
🧩

OOP Concepts

23 chapters

1Procedural vs Object-Oriented2Classes, Objects, & Instantiation3Constructors & Destructors4Static Members & Methods5Encapsulation & Access Modifiers6Data Abstraction7Inheritance Types (Single, Multiple)8Compile-Time Polymorphism (Overloading)9Polymorphism & Interfaces10Run-Time Polymorphism (Overriding)11Virtual Functions & V-Tables12Interfaces & Abstract Classes13Generic Programming (Templates & Generics)14Exception Handling in OOP15SOLID Design Principles16Composition over Inheritance17Coupling & Cohesion18UML Diagrams Basics19Creational Patterns (Singleton, Factory)20Structural Patterns (Adapter, Decorator)21Behavioral Patterns (Observer, Strategy)22MVC Architecture Pattern23Object Serialization & Cloning
SubjectsOOP Concepts

Run-Time Polymorphism (Overriding)

Updated 2026-04-21
2 min read

Run-Time Polymorphism (Overriding)

While Method Overloading allows the same method name within one class, Method Overriding allows a child class to provide a completely different implementation for a method that is already defined in its parent class. The decision of which version to call is made at runtime, not compile time.

1. Method Overriding

When a subclass has a method with the exact same signature (name, parameters, and return type) as a method in its superclass, the subclass method overrides the parent method.

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

class Dog extends Animal {
    @Override
    void speak() {
        System.out.println("The dog barks: Woof!");
    }
}

class Cat extends Animal {
    @Override
    void speak() {
        System.out.println("The cat meows: Meow!");
    }
}

Rules for Valid Overriding:

  1. The method in the child class must have the exact same name, parameters, and return type as the parent method.
  2. The access modifier of the overriding method cannot be more restrictive than the parent. If the parent method is public, the child cannot make it private.
  3. static, final, and private methods cannot be overridden.

2. Dynamic Dispatch (Why it's "Run-Time")

The truly powerful aspect of overriding is Dynamic Method Dispatch. A parent class reference variable can hold an object of any child class. The JVM decides which version of the overridden method to call at runtime based on the actual type of the object, not the declared type of the reference variable.

Animal myAnimal; // Declared type is Animal

myAnimal = new Dog();
myAnimal.speak(); // Output: "The dog barks: Woof!"

myAnimal = new Cat();
myAnimal.speak(); // Output: "The cat meows: Meow!"

The variable myAnimal is declared as type Animal, but at runtime, the JVM inspects the actual object it points to and calls the correct overridden method. This decision cannot be made at compile time because the compiler might not know which child object will be assigned to myAnimal (it could depend on user input, a database query, or a random number).

3. The Power of Polymorphism

Dynamic dispatch enables incredibly flexible and extensible code:

class AnimalShelter {
    void makeAllSpeak(Animal[] animals) {
        for (Animal a : animals) {
            a.speak(); // Correct version called automatically!
        }
    }
}

Animal[] shelter = { new Dog(), new Cat(), new Dog() };
// Output:
// The dog barks: Woof!
// The cat meows: Meow!
// The dog barks: Woof!

The makeAllSpeak method doesn't know (or care) about Dog or Cat. It only knows about Animal. If you later add a Parrot class, you don't need to change makeAllSpeak at all. The new Parrot.speak() will be automatically called via dynamic dispatch.

4. Overloading vs. Overriding Summary

FeatureOverloadingOverriding
ScopeWithin the same classBetween parent and child class
SignatureSame name, different parametersSame name, same parameters
BindingCompile-Time (Static)Run-Time (Dynamic)
PurposeConvenience (multiple ways to call)Specialization (custom behavior)


PreviousPolymorphism & InterfacesNextVirtual Functions & V-Tables

Recommended Gear

Polymorphism & InterfacesVirtual Functions & V-Tables