Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Mastering Method Overriding in Java

Mastering Method Overriding in Java

Explore Java’s method overriding with hands‑on examples, key rules, and the powerful super keyword.

Last week we covered inheritance, the core OOP feature that lets a subclass inherit properties and behaviors from a superclass. Now we’ll dive into method overriding—when a subclass supplies its own implementation of a method already defined in its parent class.

Example 1: Basic Method Overriding

class Animal {
   public void displayInfo() {
      System.out.println("I am an animal.");
   }
}

class Dog extends Animal {
   @Override
   public void displayInfo() {
      System.out.println("I am a dog.");
   }
}

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

Output:

I am a dog.

In this snippet, both Animal and Dog declare displayInfo(). When the Dog instance calls it, the subclass’s implementation runs, demonstrating overriding.

Notice the @Override annotation. It tells the compiler that the method should replace the superclass version; if the signatures don’t match, the compiler throws an error. While not mandatory, it’s a best practice that protects against accidental mistakes.

Mastering Method Overriding in Java

Java Overriding Rules

Using super to Invoke a Superclass Method

Can we still call the parent method after overriding? Absolutely—use the super keyword.

Example 2: Leveraging super

class Animal {
   public void displayInfo() {
      System.out.println("I am an animal.");
   }
}

class Dog extends Animal {
   public void displayInfo() {
      super.displayInfo();
      System.out.println("I am a dog.");
   }
}

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

Output:

I am an animal.
I am a dog.

Here, Dog first calls Animal.displayInfo() via super, then adds its own message.

Remember, constructors are not inherited, so there is no “constructor overriding.” However, a subclass can invoke a superclass constructor with super()—see the Java super keyword guide.

Access Levels When Overriding

Overridden methods can differ in visibility, but only in a way that widens access.

If a superclass method is protected, a subclass may declare it protected or public, but not private.

Example 3: Changing Visibility

class Animal {
   protected void displayInfo() {
      System.out.println("I am an animal.");
   }
}

class Dog extends Animal {
   public void displayInfo() {
      System.out.println("I am a dog.");
   }
}

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

Output:

I am a dog.

Because public offers broader access than protected, the override is valid.

Overriding Abstract Methods

Abstract classes often declare methods without bodies. Any concrete subclass must provide an implementation; otherwise, the compiler flags an error. We’ll explore abstract classes in depth in a future tutorial.


Java

  1. Java Methods: How to Define, Call, and Use Them Effectively
  2. Java Recursion: Understanding, Examples, and Trade‑Offs
  3. Mastering Java Polymorphism: Concepts, Examples, and Best Practices
  4. Java Annotation Types: A Comprehensive Guide to Predefined, Custom, and Meta Annotations
  5. Mastering Java's Iterator Interface: Practical Guide with Code Example
  6. Understanding Java String.charAt(): Syntax, Return Type, Exceptions, and a Practical Example
  7. Mastering Java Methods: Create, Invoke, and Abstraction
  8. Mastering Java Inheritance: Subclassing and Superclass Principles
  9. Java Method Overriding: Customizing Superclass Behavior
  10. Mastering Java 8: A Comprehensive Guide to Method References