Java Method Overriding

Java Overriding Class

What is Java Overriding?

  • Java Method Overriding is a mechanism by which a subclass provides its own implementation of a method that is already provided by its parent class.
  • To override a method in Java, the subclass must have the same method name, parameters, and return type as the method in the parent class.
  • When a subclass overrides a method, it provides its own implementation of that method instead of the implementation provided by its parent class.
  • To understand Java Overriding, Read the Complete Article.

Java Overriding Rules:

  • When overriding a method in Java, there are several rules that must be followed:
    • The name of the method in the subclass must exactly match the name of the method in the superclass.
    • The parameters of the method in the subclass must match the parameters of the method in the superclass.
    • The return type of the method in the subclass must be the same or a subtype of the return type of the method in the superclass.
    • The access level of the method in the subclass must be the same or less restrictive than the access level of the method in the superclass.
    • The exception thrown by the method in the subclass must be the same or a subclass of the exception thrown by the method in the superclass.
    • The method in the subclass cannot be declared as static, because static methods are not overridden, they are hidden
  • If any of these rules are violated, the code will not compile and will result in a compilation error.

Let’s look at the Java Method Overriding to perform certain operations.

Example 1: Example in Method Overriding 

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

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

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

public class Main {
    public static void main(String[] args) {
        Animal animal = new Animal();
        animal.makeSound(); // Output: Animal is making a sound
        
        Dog dog = new Dog();
        dog.makeSound(); // Output: Dog is barking
        
        Cat cat = new Cat();
        cat.makeSound(); // Output: Cat is meowing
    }
}

Output

Animal is making a sound
Dog is barking
Cat is meowing

Example 2 :Super Keyword in Java Overriding.

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

class Dog extends Animal {
    @Override
    public void makeSound() {
        super.makeSound(); // Call the makeSound() method in the superclass
        System.out.println("Dog is barking");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.makeSound();
    }
}

Output

Animal is making a sound
Dog is barking

Example 3 : Access Specifiers in Method Overriding

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

class Dog extends Animal {
    @Override
    public void makeSound() {
        super.makeSound(); // Call the makeSound() method in the superclass
        System.out.println("Dog is barking");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.makeSound();
    }
}

Output

Animal is making a sound
Animal is sleeping
Animal is eating
Dog is barking
Dog is sleeping
Dog is eating

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription