English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The benefits of overriding are: the ability to define behavior specific to subclass types, which means subclasses can implement the parent class methods according to their needs.
In object-oriented terms, overriding refers to overwriting the functionality of an existing method.
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, the reference type is checked. 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.
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.
The parameter list should be exactly the same as the list of the overridden method.
The return type should be the same as or a subtype of the return type declared in the original overridden method in the superclass.
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, it cannot be overridden.
Subclasses in the same package as the superclass can override any superclass method 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 uncheck exception. However, the overloaded method should not throw a new or broader check exception than the overloaded method declared. Compared to the overridden method, the overridden method can throw a narrower or fewer exceptions.
Constructor cannot be overridden.
method overridingSuperWhen the class version is used,superKeywords.
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