The public interface of a class is contract between the client code and the class that provides the service. Concrete classes implement each method. However, an abstract class can defer the implementation by declaring the method to be abstract, and a Java interface declares only the contract and no implementation.
The Java programming language enables Java Programmers to specify that a super class declares a method that does not supply an implementation. This is called an abstract method. The implementation of this method is supplied by the super classes.
Any class with one or more abstract method is called an abstract class. Java compiler prevents you from instantiating an abstract class. However, abstract classes can have data attributes, concrete methods, and constructors.
A concrete class implements an interface by defining all methods declared by the interface. Many classes can implement the same interface. These classes do not need to share the same class hierarchy.
Also a class can implement more than one interface. As with abstract classes, use an interface name as type of reference variable. The usual dynamic binding takes effect. References are cast to and from interface types, and you use the “instanceof” operator to determine if an object’s class implements an interface.
All methods declared in an interface are public and abstract, no matter if we explicitly mention these modifiers in the code or not. Similarly, all attributes are public, static, and final. In other words, you can declare constant attributes.
Java programmers can use interface for the following reasons
- Declare methods that one or more classes are expected to implement
- Reveal an object’s programming interface without revealing the actual body of the class. It can be useful when shipping a package of class to other developers
- Capture similarities between unrelated classes without forcing a class relationship
- Simulate multiple inheritances by declaring a class that implements several interfaces.