Method Overriding and Multi-level Hierarchy

Filter Course


Method Overriding and Multi-level Hierarchy

Published by: Dikshya

Published date: 27 Jun 2023

Method Overriding and Multi-level Hierarchy

Method overriding is a feature in object-oriented programming (OOP) that allows a subclass to provide a different implementation of a method that is already defined in its parent class. When a method is overridden, the version of the method defined in the subclass is executed instead of the version inherited from the parent class.

To override a method, the subclass must declare a method with the same name, return type, and parameters as the method it wants to override. The method in the parent class must also be declared with the virtual, abstract, or override keyword (depending on the programming language) to indicate that it can be overridden.

When an object of the subclass calls the overridden method, the program looks for the implementation in the subclass first. If an override is found, the subclass's implementation is executed. If no override is found, the program looks for the method in the parent class.

Method overriding is commonly used in scenarios where a subclass needs to provide its own implementation of a method inherited from its parent class. This allows for customization and specialization of behavior based on the specific requirements of the subclass. It is an important feature for achieving polymorphism, where objects of different classes can be treated uniformly through a common interface.

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

class Cat extends Animal {
    @Override
    public void makeSound() {
        System.out.println("The cat says meow");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal animal = new Animal();
        animal.makeSound(); // Output: The animal makes a sound

        Cat cat = new Cat();
        cat.makeSound(); // Output: The cat says meow

        Animal animalCat = new Cat();
        animalCat.makeSound(); // Output: The cat says meow
    }
}
In the example above, the Animal class has a method makeSound(). The Cat class extends Animal and overrides the makeSound() method with its own implementation. When we create an object of type Cat and call the makeSound() method, the overridden version in the Cat class is executed. Even when we assign the Cat object to a variable of type Animal and call makeSound(), the overridden method is still invoked because the actual object type is a Cat.

Multi-level hierarchy:

Multi-level hierarchy, also known as inheritance, is a fundamental concept in object-oriented programming (OOP) where classes are organized in a hierarchical structure. In this hierarchy, classes can inherit properties and behaviors from other classes, creating a "parent-child" relationship between them.

In multi-level hierarchy, a class can inherit from another class, which itself can be a subclass of another class, forming a chain or hierarchy of classes. The topmost class in the hierarchy is often called the "base class," "superclass," or "parent class." The classes that inherit from the base class are referred to as "derived classes," "subclasses," or "child classes."

The main purpose of multi-level hierarchy is to promote code reuse and modularity. By defining common attributes and behaviors in a base class, derived classes can inherit and extend them, avoiding redundant code and facilitating maintenance. Each level in the hierarchy adds more specific attributes and behaviors to the classes.

class Animal {
    void eat() {
        System.out.println("The animal eats.");
    }
}

class Mammal extends Animal {
    void run() {
        System.out.println("The mammal runs.");
    }
}

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

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.eat(); // Output: The animal eats.
        dog.run(); // Output: The mammal runs.
        dog.bark(); // Output: The dog barks.
    }
}