Summary: In this tutorial, you will learn about method overriding in Java with the help of the examples.

Method Overriding in Java

In Java, method overriding occurs when a subclass defines a method with the same signature as a method in its superclass. When this happens, the subclass method is said to override the superclass method.

Here is an example of method overriding in Java:

class Animal {
  public void makeSound() {
    System.out.println("Some generic animal sound");
  }
}

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

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

public class Main {
  public static void main(String[] args) {
    Animal dog = new Dog();
    Animal cat = new Cat();
    
    dog.makeSound(); // Outputs: Bark!
    cat.makeSound(); // Outputs: Meow!
  }
}

In this example, there is a superclass called Animal that has a single method called makeSound(). There are two subclasses of Animal, called Dog and Cat, that override the makeSound() method and provide their own implementation.

When the makeSound() method is called on an instance of the Dog class, the version of the method defined in the Dog class is executed, which outputs “Bark!”.

Similarly, when the makeSound() method is called on an instance of the Cat class, the version of the method defined in the Cat class is executed, which outputs “Meow!”.

This demonstrates how method overriding allows subclasses to provide their own implementation of a method that is defined in their superclass.

@Override Annotation

Notice the @Override annotation in the above example. It is used to indicate that a method is intended to override a method in a superclass.

If it is used on a method that is not actually overriding a method in a superclass, the Java compiler will generate an error.

This can be helpful in catching mistakes, such as misspelling the name of the method or using the wrong number or types of parameters.

Using the @Override annotation is not strictly required in Java, but it is a good practice that can help to improve the quality and readability of your code.

Rules for Method Overriding

There are a few rules that must be followed when overriding a method in Java:

  1. The subclass method must have the same signature as the superclass method. This means that the subclass method must have the same name, number of parameters, and types of parameters as the superclass method.
  2. The subclass method must have the same return type or a subtype of the superclass method’s return type.
  3. The subclass method must not have a more restrictive access modifier than the superclass method. For example, if the superclass method is public, the subclass method cannot be private or protected.
  4. The subclass method can throw any unchecked exceptions, regardless of whether the superclass method throws any exceptions. However, the subclass method cannot throw any checked exceptions that are not declared by the superclass method.
  5. If the superclass method is final, static, or private, it cannot be overridden.

It’s important to follow these rules when overriding a method in order to avoid compile-time errors and to ensure that your code behaves as intended.

Here is an example of a method override in Java that does not follow the rules:

class Animal {
  public void makeSound() {
    System.out.println("Some generic animal sound");
  }
}

class Dog extends Animal {
  @Override
  private void makeSound() { // This is not allowed because the subclass method has a more restrictive access modifier than the superclass method
    System.out.println("Bark!");
  }
}

In this example, the makeSound() method in the Dog class is annotated with @Override, which indicates that it is intended to override the makeSound() method in the Animal class.

However, the makeSound() method in the Dog class has a private access modifier, which is more restrictive than the public access modifier of the makeSound() method in the Animal class.

This is not allowed according to the rules of method overriding, as the subclass method must not have a more restrictive access modifier than the superclass method. As a result, the Java compiler will generate an error when this code is compiled.

Use of super Keyword in Method Overriding

In Java, the super keyword is used to refer to the instance of the superclass that is associated with a subclass object. It is often used in method overrides to call the implementation of a method defined in the superclass.

Here is an example of using the super keyword in a method override in Java:

class Animal {
  public void makeSound() {
    System.out.println("Some generic animal sound");
  }
}

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

In this example, the makeSound() method in the Dog class overrides the makeSound() method in the Animal class. The makeSound() method in the Dog class calls the makeSound() method in the Animal class using the super keyword, and then prints out “Bark!”.

This allows the Dog class to reuse the implementation of the makeSound() method in the Animal class and extend it by adding additional behavior.

The super keyword can also be used to access other members of the superclass, such as fields and constructors. It is a useful tool for accessing the functionality of the superclass from within a subclass.

Overriding Abstract Methods

It is mandatory to override an abstract method in a subclass if the subclass is not itself abstract.

An abstract method is a method that is declared in a superclass, but does not have an implementation. Abstract methods must be implemented by any subclass that does not itself declare the method as abstract.

Here is an example of a subclass that does not override an abstract method:

abstract class Animal {
  public abstract void makeSound();
}

class Dog extends Animal {
  // No implementation of the makeSound() method
}

In this example, the Animal class declares the makeSound() method as abstract, which means that it must be implemented by any subclass that is not itself abstract.

The Dog class is a concrete subclass of Animal, but it does not provide an implementation for the makeSound() method.

As a result, the Java compiler will generate an error when this code is compiled, because the Dog class is a concrete subclass that does not override the abstract method defined in its superclass.

It is therefore mandatory to override an abstract method in a subclass if the subclass is not itself abstract. This allows the subclass to provide its own implementation of the abstract method and ensure that it is properly implemented.

You will learn more about abstract class and super keyword in detail in later tutorials.


Leave a Reply