JAVA rewrite and @ Override label, java rewrite @ override

Source: Internet
Author: User

JAVA rewrite and @ Override label, java rewrite @ override

I used to read less in JAVA and recently made a project. I had some questions about the calling sequence of @ Override, so I checked some information. Now that you have checked the information, sort out the knowledge points you can see for future study.

About Rewriting

When a subclass inherits a parent class, and the names, numbers, and types of methods in the subclass are identical to those in the parent class, this method in the subclass overwrites the method in the parent class. Generally, a derived class inherits the methods of the base class. Therefore, when an Object Inheritance method is called, the implementation of the base class is called and executed. however, it is sometimes necessary to implement different inheritance methods in the derived class.
For example, assume that the animal class has a "run" method, from which the horse and the dog are derived. The running forms of the horse and the dog are different. Therefore, the same method requires two different implementations, this requires "re-writing" methods in the base class. The "Override" base class method is to modify its implementation or rewrite it in the derived class.

Rewriting is a type of polymorphism between child classes and parent classes.
Override allows subclass to change some behavior of the parent class. When the parent class does not meet the requirements of the subclass, we need the subclass to override some behavior of the parent class.

Rewrite example

For example, the phone numbers of employees in a company cannot be disclosed, while those of sales personnel (employees) must be disclosed.

 

1 public class Employee {2 3 private String mobile; 4 5 public Employee (String mobile) {6 this. mobile = mobile; 7} 8 9 protected String showMess () {10 return "phone number:" + mobile; 11} 12}

 

The showMess method of the employee class is protected, so the objects in other packages cannot be accessed.

Then define a Sales personnel class and inherit the Employee class

1 public class Sales extends Employee {2 3 // In addition to some attributes of the parent class, the subclass can also have its own attributes 4 private String msn; 5 6 public Sales (String mobile, string msn) {7 super (mobile); 8 this. msn = msn; 9} 10 11 @ Override12 public String showMess () {13 return super. showMess () + "= msn:" + this. msn; 14} 15}

Note that the access level of the overwritten showMess method is public and can be accessed by any other object.

 

Key Points

1. The access control level of the method to be overwritten can be different.

For example, in the previous example, the access level of the showMess method of the parent class is protected, and the access level of the showMess method covered by the subclass is public.
However, the subclass access level must be higher than the access level of the method covered by the parent class. It is incorrect if the parent class is public and the subclass is protected.

2. A method defined as private, static, or final cannot be overwritten.

3. Return type of the method.

As mentioned above, the names, parameters, and types of the override methods must be consistent, but the return type is not specified. Here, the return types can be different, but the types are limited.

The return type of the subclass can be a more specific Object. For example, it is also correct to change the return type of the Employee class to the Object type. In turn, this is an error.

4. When a method is called, the override method is first found in the subclass. If the subclass does not exist, the method is found in the parent class.

 

Call Sequence example

 1 public class Parent { 2      3     private int num(int i,int j){ 4         return i+j; 5     } 6      7     public static void main(String[] args) { 8         Parent p = new Child(); 9         System.out.println(p.num(1, 2));10     }11 }12 class Child extends Parent{13     14     public int num(int x,int y){15         return x-y;16     }17 }

Why is the execution result of this code? If you answer-1, it is wrong. The correct answer is 3.
Why? Because the num method of the parent class is private, it cannot be overwritten. Therefore, the num method of the subclass is not an Override, therefore, if the subclass does not find the overwritten num method, the num method of the parent class will be executed. Therefore, the output result is 3.

1 public class Parent {2 3 public int test () {4 // execute the num method of the subclass 5 return num (1, 2); 6} 7 8 protected int num (int I, int j) {9 return I + j; 10} 11 12 public static void main (String [] args) {13 Parent p = new Child (); 14 System. out. println (p. test (); 15} 16 17} 18 class Child extends Parent {19 20 public int num (int x, int y) {21 return x-y; 22} 23}

The execution result of this Code is-1, because the test method of the parent class calls the num method of the subclass.

Let's look at the next example and first write a base class:

 1 package tttttest; 2  3 public class Fruit { 4     public void show_name(int num) { 5         System.out.println("Fruit: " + num); 6     } 7  8     public static void main(String[] args) { 9         // TODO code application logic here10         Fruit apple = new Apple(); 11         apple.show_name(2);12     }13 }

Then we compile an Apple subclass to inherit this base class. And rewrites the show_name () method in the base class.

 1 package tttttest; 2  3 public class Apple extends Fruit{ 4  5     @Override 6     public void show_name(int num) { 7         // TODO Auto-generated method stub 8         System.out.println("Apple: " + num); 9     }10 11 }

The output result is Apple: 2.

 

@ Override tag

@ Override is a pseudo-code that indicates rewriting.

As a comment

Second, to increase the readability of the code, you can see that the label is the method of rewriting from the parent class.

Third, IDE will check your code based on this label. If you do not pay too much attention to rewrite specifications when writing code and make the four key points mentioned above, the IDE will help you mark errors if you add this tag.

 

How to enable the IDE to automatically add @ Override labels

In Eclipse, right-click the editor and choose Source> Override/Implement Methods.

Select the method to be rewritten from the list.

 

References

Java polymorphism Override

Share the little secret behind the @ Override tag-record java's integration of Thinking and Practice

 


Keyword override in Java !!!

Override is a method override. It usually occurs in the subclass and parent classes. It refers to a method defined in the subclass that has the same type as the return value of the parent class and has the same parameter type.
This is not a keyword !! Override can be added when the subclass overrides the parent class method, or not, but it should be @ override

What should I do if the @ Override label is not found when the method is automatically added to MyEclipse?

There is no relationship between them. If you select the override option from the source menu to allow the system to add code for you, there will be tags. You do not need the tag if you write it yourself.

Related Article

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.