The following is referenced from http://wiki.jikexueyuan.com/project/java/overriding.html:
If a class inherits a method from its parent class, this method can be overridden if the method is not marked as final.
The benefit of overriding is the ability to define behavior that is specific to a subclass type, which means that subclasses can implement methods of the parent class based on the requirements.
In object-oriented programming, the overriding method means rewriting a method that already exists.
Example:
Take a look at the following example:
classanimal{ Public voidMove () {System.out.println ("Animals can Move"); }}classDogextendsanimal{ Public voidMove () {System.out.println ("Dogs can walk and run"); }} Public classtestdog{ Public Static voidMain (String args[]) {Animal a=NewAnimal ();//Animal Reference and ObjectAnimal B =NewDog ();//Animal Reference But Dog objectA.move ();//runs the method in Animal classB.move ();//Runs the method in Dog class }}//this will produce the following results:Animals can Movedogs can walk and run
In the example above, you can see that although B is a animal type, it runs the method of the dog class. The reason is that the reference type is checked at compile time. However, at run time, the JVM determines which object the object type belongs to.
Therefore, in the above example, although animal has a move method, the program compiles normally. At run time, a method that runs a particular object.
Consider the following example:
classanimal{ Public voidMove () {System.out.println ("Animals can Move"); }}classDogextendsanimal{ Public voidMove () {System.out.println ("Dogs can walk and run"); } Public voidbark () {System.out.println ("Dogs can Bark"); }} Public classtestdog{ Public Static voidMain (String args[]) {Animal a=NewAnimal ();//Animal Reference and ObjectAnimal B =NewDog ();//Animal Reference But Dog objectA.move ();//runs the method in Animal classB.move ();//Runs the method in Dog classB.bark (); }}//this will produce the following results:Testdog.java:30: Cannot find Symbolsymbol:method bark () Location:classAnimal B.bark ();
This program will throw an error at compile time because the reference type of B animal does not have a method named bark.
First, Method rewrite rules
- The parameter list of the overridden method should be exactly the same as the original method.
- The return value type should be the same as the return value type of the original method or its subtype when the parent class is defined.
- The Override function access level limit cannot be higher than the original function. For example: If the parent method is declared as public, the overriding method in the subclass cannot be private or protected.
- The method can be overridden only if the quilt class inherits.
- method is defined as final and will cause it to not be overridden.
- A method is defined as static, which prevents it from being rewritten, but can be re-declared.
- A method cannot be inherited, and it cannot be overridden.
- And the subclass of the parent class in a package can override any parent class method that is not declared private and final.
- Subclasses that are not in the same package as the parent class can only override the Non-final method or a method that is declared public or protected.
- An overriding method can throw any run-time exception, regardless of whether the overridden method throws an exception. However, the overriding method should not throw a more extensive check exception than the update that is overridden by the method declaration. Overriding methods can throw exceptions that are narrower or fewer than overridden methods.
- Constructors cannot be overridden.
Second, use super keyword
When calling the overridden method of the parent class, use the keyword super.
classanimal{ Public voidMove () {System.out.println ("Animals can Move"); }}classDogextendsanimal{ Public voidMove () {Super. Move ();//invokes the Super class methodSystem.out.println ("Dogs can walk and run"); }} Public classtestdog{ Public Static voidMain (String args[]) {Animal b=NewDog ();//Animal Reference But Dog objectB.move ();//Runs the method in Dog class }}//this will produce the following results:Animals can Movedogs can walk and run
Test Project: https://github.com/easonjim/5_java_example/tree/master/javabasicstest/test18
Overrides in Java