@override tags that java often encounters

Source: Internet
Author: User

Whether in NetBeans or in the Eclipse development environment, when writing Java code often encounter @override tags, usually only know that the meaning of coverage is good ~ then the value of this tag is only these?

Most recently, when I re-turned the thinking in Java, I looked at it, and the small label deepened my understanding of object-oriented.

General Purpose

    • Help yourself to check if the correct replication of the existing methods in the parent class
    • Tell the person reading the code that this is a replication method

For example, we have the following base classes

1 package fruit;
2/**
3 * @author Octobershiner
4 */
5 public class Fruit {
6
7 Public void show_name (int num) {
8 System.out.println ("Fruit:" +mum);
9 }
10
Public static void Main (string[] args) {
//TODO code application logic here
Fruit Apple = new Apple ()//generate a kind of new Fruit
Apple.show_name (2);
}
16}

We then write an Apple subclass that inherits the base class. and the Show_name () method in the replication base class.

1 package fruit;

3 public class Apple extends fruit{

5
6 @Override
7 Public void show_name (int num) {
8 System.out.println ("Apple");
9 }
10}

The result of the execution, it is obvious that will print out apple:2 words.

In fact, when we hand-copy the method of the parent class, it is easy to remember the parameters of the method, if you do not add @override at this time, the editor will not prompt you: for example, we do not add this tag, quietly changed the parameters to float type.

This time, in fact, we did not follow our intent to successfully copy the method, so a hidden bug was born, and instead of the effect of override is

The IDE gives an error stating that our replication method failed.

This is often the result of preparing a replication method, instead we are overloading the method.

A thought of Bruce

Bruce, author of Thinking in Java, discusses this issue by mentioning an example of the override private approach:

Now we're adding a private method to the fruit class, and we're trying to replicate it in Apple

1         @Override
2 private void Grow () {
3 }

The compiler will prompt for an error, which is a very low-level error, but sometimes it's just not something we find out: it's trying to replicate a private method, but when we remove the override tag, the compiler doesn't give an error and it can be executed.

in fact, your so-called grow in Apple is just a private approach to Apple itself. is a completely new approach.

This begs the question, what is a carbon copy?

In object-oriented, only the interface and common methods, the inheritance method is only a replication, private methods can not be reviewed, but also think about it, just understand: not to review, but there is no concept of the private method of replication!

This is the object-oriented design of the original intention, the private method itself is to encapsulate within the class, do not want others to change or external references, see here, suddenly feel, Java design is really good, feel the idea and the realization of unity.

Previously always felt that the override tag is dispensable, but did not expect to elicit so many problems, and then get a revelation: seriously thinking about the meaning of every grammatical detail, think line unity, the end of the article worship Bruce.

@override tags that java often encounters

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.