An interface is basically a kind of class. Like classes, interfaces contain methods and variables but with a major difference. The difference is that interfaces define only abstract methods and final fields. This means that interface do not specify any code to implement these methods and data fields contain only the constants. Therefore it is the responsibility of the class that implements an interface to define the code for implementation of these methods. An interface is a contract for what a class can do, without saying anything about how the class will do it. Interfaces are like a 100-percent abstract super class that defines the methods a subclass must support, but not how they must be supported. The syntax for defining an interface is as follows:
An example for an interface is: public interface Shape { Extending interfaces public class Square extends Rectangle { Implementing interfaces Interfaces are used as “super classes” whose properties are inherited by classes. It is therefore necessary to create a class that inherits that inherits the given interface. public class Rectangle implements Area { class InterfaceTest { } Any number of dissimilar classes can implement an interface. Note that if a class implements an interface does not implement all the methods of the interface, then the class becomes an abstract class and cannot be instantiated. |