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
;