Why can't interface & abstract class contain static methods!

Source: Internet
Author: User

Why can't interface & abstract class contain static methods!
Abstract class {
Static void dosomething ();
}
It cannot be compiled because you have not defined the dosomething method body. For example, if you add {} to the end of the statement, you can use it. At this time, dosomething is a class method of.
If you want to define it as an abstract method, you must use the keyword abstract when defining it. if you want to define a method and use the keywords abstract and static at the same time, this cannot be done. you can think about it. The static method is a class method, which does not allow subclass override (so it must be implemented during definition). The abstract method only has the feature mark, the methods that are not implemented must be overwritten by sub-classes. Sun certainly does not allow defining abstract and static methods at the same time.

Interfaces and abstract classes are a little different because they are purely designed and do not involve any implementations. All their methods are common and abstract (the public and abstract keywords can be omitted) therefore, the interface cannot have static methods (class methods). The static void dosomething () {}; mentioned above can exist in the abstract class, but is not allowed in the interface.

By the way, attributes of Staic can be defined in the interface. In fact, the attributes defined in the interface must be static, final, and public (because they are required, these three keywords can also be omitted)

Original Then I: Static Yong Far Cannot match Abstract Touch Header ;
Original Then II: Interface Is a more abstract Abstract Class , None of the methods of the interface Implementation , Interface Method Mo Think Public Abstract , According to the original Then I. Of course it cannot be Static Now;
Original Then Iii. Abstraction Class Yes Implementation , Where Non - Abstract The method is acceptable. Tape Static , Abstract The method cannot Tape Static ;

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.