|
Contributed by Teddy
|
|
Thursday, 28 September 2006 |
What Is a Java Interface?Class is the fundamental unit of programming in the Java programming , type is the fundamental unit of object-oriented design.While classes define types, it is very useful and powerful to be able to define a type without defining a class. Interfaces define types in an abstract form as a group of related methods with empty bodies
A Simple Interface ExampleAn interface declaration is similar to a class declaration, except that the keyword interface is used instead of class. See following example: public interface Playable { void play(); } Implementing an interfaceTo implement an interface, a class must do two things:For example, here’s a class that implements the Playable interface: public class Basketball implements Playable { public void play() { } }
Differences between Interfaces and inheritanceThe interface itself doesn’t provide code that implements any of its methods. An interface is just a set of method and field signatures. In contrast,a base class can provide the implementation for some or all of its methods. A class can have only one base class. However, a class can implement as many interfaces as necessary. |
|
Last Updated ( Thursday, 28 September 2006 )
|