Inheritance and Method Overriding in Java


In Java, inheritance is a mechanism where one class acquires the properties (fields) and behaviors (methods) of another class. Inheritance helps to promote reusability and establish relationships between classes. Method overriding is a feature that allows a subclass to provide a specific implementation of a method that is already defined in its superclass.

1. Inheritance in Java

Inheritance in Java is achieved using the extends keyword. A class that is inherited from is called a superclass, and the class that inherits is called a subclass.

Example 1: Basic Inheritance

In this example, the class Animal is the superclass, and the class Dog is the subclass that inherits from Animal.

    class Animal {
        void eat() {
            System.out.println("This animal eats food.");
        }
    }
    
    class Dog extends Animal {
        void bark() {
            System.out.println("The dog barks.");
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Dog dog = new Dog();
            dog.eat();  // Inherited method
            dog.bark(); // Method of Dog class
        }
    }
        

Output:

    This animal eats food.
    The dog barks.
        

In this example, the Dog class inherits the eat() method from the Animal class.

2. Method Overriding in Java

Method overriding in Java occurs when a subclass defines a method that has the same signature (name, return type, and parameters) as a method in the superclass. The overriding method in the subclass provides a specific implementation of the method.

Method overriding is done using the same method signature, and it must be defined inside the subclass.

Example 2: Method Overriding

In this example, we override the eat() method in the Dog class to provide a more specific implementation:

    class Animal {
        void eat() {
            System.out.println("This animal eats food.");
        }
    }
    
    class Dog extends Animal {
        @Override
        void eat() {
            System.out.println("The dog eats bones.");
        }
    
        void bark() {
            System.out.println("The dog barks.");
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Animal animal = new Animal();
            animal.eat();  // Calls the Animal class eat() method
    
            Dog dog = new Dog();
            dog.eat();  // Calls the overridden Dog class eat() method
            dog.bark(); // Method of Dog class
        }
    }
        

Output:

    This animal eats food.
    The dog eats bones.
    The dog barks.
        

In this example, the Dog class overrides the eat() method of the Animal class, and provides its own implementation for how a dog eats.

3. Why Use Method Overriding?

Method overriding allows a subclass to implement a method that is specific to its behavior, even though it has the same name and signature as a method in the superclass. This makes it easier to use polymorphism, where a superclass reference can refer to a subclass object, and the correct method is called based on the actual object type.

Example 3: Polymorphism with Method Overriding

In this example, we demonstrate how method overriding enables polymorphism:

    class Animal {
        void sound() {
            System.out.println("Animals make sounds.");
        }
    }
    
    class Dog extends Animal {
        @Override
        void sound() {
            System.out.println("The dog barks.");
        }
    }
    
    class Cat extends Animal {
        @Override
        void sound() {
            System.out.println("The cat meows.");
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Animal animal1 = new Dog();
            animal1.sound();  // Calls Dog's sound() method
    
            Animal animal2 = new Cat();
            animal2.sound();  // Calls Cat's sound() method
        }
    }
        

Output:

    The dog barks.
    The cat meows.
        

Here, both Dog and Cat override the sound() method from the superclass Animal. The method called depends on the type of object (Dog or Cat) even though the reference is of type Animal.

4. Using super Keyword to Call Superclass Methods

If you want to call a method from the superclass inside an overridden method in the subclass, you can use the super keyword. This is useful when you want to extend the behavior of the superclass method and add more functionality in the subclass.

Example 4: Using super to Call a Superclass Method

In this example, we call the eat() method from the superclass Animal inside the overridden method of the subclass Dog:

    class Animal {
        void eat() {
            System.out.println("This animal eats food.");
        }
    }
    
    class Dog extends Animal {
        @Override
        void eat() {
            super.eat();  // Call the superclass method
            System.out.println("The dog eats bones.");
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Dog dog = new Dog();
            dog.eat();  // Calls the Dog class eat() method, which calls the Animal class eat() method
        }
    }
        

Output:

    This animal eats food.
    The dog eats bones.
        

In this case, we use super.eat() to call the eat() method of the superclass before adding the subclass-specific behavior.

5. Conclusion

Inheritance and method overriding are key concepts in object-oriented programming (OOP) that help in creating flexible and reusable code. Inheritance allows a subclass to inherit properties and methods from a superclass, while method overriding allows a subclass to provide a specific implementation for a method that is already defined in the superclass. Together, these concepts enable the use of polymorphism, which is crucial for writing more maintainable and extensible Java code.





Advertisement