English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Overriding methods of different methods in Java

After testing

The benefits of overriding are that it allows for defining behavior specific to subclass types, which means subclasses can implement the parent class methods according to their requirements.

In object-oriented terms, overriding refers to overwriting the functionality of an existing method.

Example

class Animal {
   public void move() {
      System.out.println("Animals can move");
   }
}
class Dog extends Animal {
   public void move() {
      System.out.println("Dogs can walk and run");
   }
}
public class TestDog {
   public static void main(String args[]) {
      Animal a = new Animal(); // Animal reference and object
      Animal b = new Dog(); // Animal reference but Dog object
      a.move(); // runs the method in Animal class
      b.move(); // runs the method in Dog class
   }
}

Output Result

Animals can move
Dogs can walk and run

In the above example, you can see that evenbis a type of Animal, and it will also run the move method in the Dog class. The reason is: at compile time, type checks are performed on reference types. However, at runtime, the JVM determines the object type and runs the method that belongs to the specific object.

Therefore, in the above example, since the Animal class has the method move, the program will compile correctly. Then, at runtime, it will run the method specific to the object.

Example

class Animal {
   public void move() {
      System.out.println("Animals can move");
   }
}
class Dog extends Animal {
   public void move() {
      System.out.println("Dogs can walk and run");
   }
public void bark() {
      System.out.println("Dogs can bark");
   }
}
public class TestDog {
   public static void main(String args[]) {
      Animal a = new Animal(); // Animal reference and object
      Animal b = new Dog(); // Animal reference but Dog object
      a.move(); // runs the method in Animal class
      b.move(); // runs the method in Dog class
      b.bark();
   }
}

Output Result

TestDog.java:26: error: cannot find symbol
b.bark();
^
symbol: method bark() location: variable b of type Animal
1 error

Since the reference type Animal of b does not have a bark name method, the program will cause a compile-time error.

Method Overriding Rules

  • The parameter list should be exactly the same as the list of the overridden method.

  • The return type should be the same as the return type of the original overridden method in the superclass or a subtype of that subtype.

  • The access level cannot be stricter than the access level of the overridden method. For example: if the superclass method is declared as public, then the overridden method in the subclass cannot be private or protected.

  • Instance methods can only be overridden if the subclass inherits.

  • Final methods cannot be overridden.

  • Static methods cannot be overridden but can be redeclared.

  • If a method cannot be inherited, then the method cannot be overridden.

  • Subclasses in the same package as the instance's superclass can override any superclass method that is not declared as private or final.

  • Subclasses in different packages can only override non-final methods declared as public or protected.

  • Whether the overridden method throws an exception or not, the overridden method can throw any unchecked exception. However, the overloaded method should not throw a new or broader checked exception than the overloaded method declares. Compared to the overridden method, the overridden method can throw a narrower or fewer exceptions.

  • Constructor cannot be overridden.

Use the super keyword

To call the overridden method,SuperWhen a class version is used,superKeywords.

Example

class Animal {
   public void move() {
      System.out.println("Animals can move");
   }
}
class Dog extends Animal {
   public void move() {
      super.move(); // invokes the super class method
      System.out.println("Dogs can walk and run");
   }
}
public class TestDog {
   public static void main(String args[]) {
      Animal b = new Dog(); // Animal reference but Dog object
      b.move(); // runs the method in Dog class
   }
}

Output Result

Animals can move
Dogs can walk and run

Test