JAVA class and object (8) ----- rewriting, java class ----- Rewriting

Source: Internet
Author: User

JAVA class and object (8) ----- rewriting, java class ----- Rewriting

Rewrite: The subclass re-writes the implementation process of the methods allowed to access the parent class! Neither the return value nor the form parameter can be changed. That is, the shell remains unchanged and the core is overwritten!

Benefit: You can define your own behaviors according to the requirements of subclass. That is to say, sub-classes can implement the parent class method as needed.

Class Animal {public void move () {System. out. println ("animals can be moved");} class Dog extends Animal {public void move () {System. out. println ("dogs can run and walk");} public class AnimalThree {public static void main (String args []) {Animal a = new Animal (); // Animal object Animal B = new Dog (); // Dog object. move (); // Method B that executes the Animal class. move (); // Method for executing the Dog class }}

In the above example, although B belongs to the Animal type, it runs the move method of the Dog class.

This is because only the reference type of the parameter is checked during the compilation phase.

However, during runtime, the Java Virtual Machine (JVM) specifies the object type and runs the object's method.

Therefore, in the above example, the reason for successful compilation is that the Animal class has the move method, but the running method is the method of a specific object.

 

The following example is incorrect:

Class Animal {public void move () {System. out. println ("animals can be moved");} class Dog extends Animal {public void move () {System. out. println ("dogs can run and walk");} public void bark () {System. out. println ("dogs can bark") ;}} public class AnimalFour {public static void main (String args []) {Animal a = new Animal (); // Animal object Animal B = new Dog (); // Dog object. move (); // Method B that executes the Animal class. move (); // Method B for executing the Dog class. bark ();}}

This example cannot be compiled. You should change the object B declaration to "Dog B = new Dog ();". The correct result is:

 

 

Method rewriting rules
  • The parameter list must be exactly the same as that of the method to be overwritten;
  • The return type must be exactly the same as the return type of the method to be overwritten;
  • The access permission cannot be lower than the access permission of the override method in the parent class. For example, if a method of the parent class is declared as public, you cannot declare this method as protected if you rewrite it in the subclass.
  • The member method of the parent class can only be overwritten by its subclass.
  • Methods declared as final cannot be overwritten.
  • Methods declared as static cannot be overwritten, but can be declared again.
  • The subclass and its parent class are in the same package. Then, the subclass can override all the methods of the parent class except the methods declared as private and final.
  • The subclass and the parent class are not in the same package. Therefore, the subclass can only override the non-final method declared as public and protected for the parent class.
  • The method to be rewritten can throw any non-forced exception, regardless of whether the method to be rewritten throws an exception. However, a method to be rewritten cannot throw a new mandatory exception, or be more extensive than a mandatory exception declared by the method to be rewritten. Otherwise, it can.
  • The constructor cannot be overwritten.
  • If you cannot inherit a method, you cannot override this method.

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.