@ Override problem in Java and Solution

Source: Internet
Author: User

Let's first look at a code snippet:

public class CommonEaoImpl extends BasicEao implements ICommonEao {@Overridepublic void save(Object entity) {em.persist(entity);}@Overridepublic 
 
   void saveList(List
  
    entitys) {for (Object entity : entitys) {em.persist(entity);}}}
  
 

From the Code, there is no error, but the error is as follows:

The method save(Object) of type CommonEaoImpl must override a superclass method
The Code does not need to be explained more. A simple translation of errors: the parent class must have a save method before the subclass can be rewritten. The current situation is that although BasicEao. java does not have the save method, there is a save statement in ICommonEao:
void save(Object entity);
It is easy to solve this problem simply. Remove @ Override from the subclass to compile the code correctly. But I think we can think a little more: Why can we just delete it?

After checking, we found that the error is not in the code, but in jdk, the code can be correctly compiled in jdk 1.6, but in jdk, the @ Override error is reported because: in jdk1.5, @ Override applies only to class inheritance, but not to interface implementation. In jdk1.6, @ Override supports both implementation and inheritance.

So now there are two solutions:

I declared save in the ICommonEao interface to change jdk to 1.6 in BasicEao of the parent class. In order to confirm this, I also went to the API documentation (jdk1.5, jdk1.6) on the official website, this difference is not shown in official documents. Both are:

Indicates that a method declaration is intended to override a method declaration in a superclass. if a method is annotated with this annotation type but does not override a superclass method, compilers are required to generate an error message.
After learning English during this period, I think you can try to translate it.

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.