只问耕耘
分类: Java
2010-01-14 14:41:54
As you already know, a class in Java can extend only one other class. However, a Java class can implement any number of interfaces. (Multiple inheritance is allowed with interfaces.)
Similar to an abstract class
An interface is similar, but not identical, to an abstract class.
(An abstract class may contain abstract methods or concrete methods, or a combination of the two. However, an abstract class cannot be instantiated. An interface also cannot be instantiated.)For example, all methods declared in an interface must be (are implicitly) abstract, but that is not true for an abstract class. An abstract class can also contain fully-defined (concrete) methods.
A totally abstract class
At the risk of offending the purists, I will coin a new term here and say that an interface is similar to a totally abstract class (one which contains only abstract method declarations and final variables).
The cardinal rule
The cardinal rule in implementing interfaces is:
If a class implements an interface, it must provide a concrete definition for all the methods declared by that interface, and all the methods inherited by that interface. Otherwise, the class must be declared abstract and the definitions must be provided by a class that extends the abstract class.