Java classes and Objects (eight)-----overrides

Source: Internet
Author: User

Rewrite: Subclasses re-write the implementation of the method that allows access to the parent class! Neither the return value nor the formal parameter can be changed. That is: The shell does not change, the core rewrite!

Benefit: You can define your own behavior, depending on the needs of the subclass. That is, subclasses can implement methods of the parent class as needed.

classanimal{ Public voidMove () {System.out.println ("Animals can move."); }}classDogextendsanimal{ Public voidMove () {System.out.println ("Dogs can run and walk."); }} Public classanimalthree{ Public Static voidMain (String args[]) {Animal a=NewAnimal ();//Animal ObjectAnimal B =NewDog ();//Dog ObjectA.move ();//methods for executing the Animal classB.move ();//methods for executing the Dog class   }}

As you can see in the example above, although B belongs to the animal type, it runs the Move method of the dog class.

This is because in the compile phase, only the reference type of the parameter is checked.

At run time, however, the Java Virtual machine (JVM) specifies the type of object and the method that runs the object.

So in the example above, the reason for compiling successfully is because the Move method exists in the animal class, but at run time, the method of the particular object is running.

The following example is wrong:

classanimal{ Public voidMove () {System.out.println ("Animals can move."); }}classDogextendsanimal{ Public voidMove () {System.out.println ("Dogs can run and walk."); }    Public voidbark () {System.out.println ("Dogs can bark."); }} Public classanimalfour{ Public Static voidMain (String args[]) {Animal a=NewAnimal ();//Animal ObjectAnimal B =NewDog ();//Dog ObjectA.move ();//methods for executing the Animal classB.move ();//methods for executing the Dog classB.bark (); }}

This example compilation fails, the object B declaration should be changed to "Dog B=new Dog ();", the correct result is:

overriding rules for methods
    • The argument list must be exactly the same as the overridden method;
    • The return type must be exactly the same as the return type of the overridden method;
    • Access permissions cannot be lower than the overridden methods in the parent class. For example, if a method of a parent class is declared public, overriding the method in a subclass cannot be declared as protected.
    • A member method of a parent class can only be overridden by its subclasses.
    • A method that is declared final cannot be overridden.
    • A method declared as static cannot be overridden, but can be declared again.
    • Subclasses and parent classes in the same package, subclasses can override all methods of the parent class, except for the methods declared private and final.
    • Subclasses and parent classes are not in the same package, the subclass can only override non-final methods that are declared public and protected by the parent class.
    • The overridden method can throw any non-mandatory exception, regardless of whether the overridden method throws an exception. However, the overridden method cannot throw a new mandatory exception, or a more extensive mandatory exception than the overridden method declaration, or vice versa.
    • The construction method cannot be overridden.
    • If you cannot inherit a method, you cannot override this method.

Java classes and Objects (eight)-----overrides

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.