Interface
Abstract class is used for achieving partial abstraction. Unlike abstract class an interface is used for full abstraction. Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user. A class extends another class, an interface extends another interface but a class implements an interface.
An interface can have methods and variables just like the class but the methods declared in interface are by default abstract (only method signature, no body). Variables declared in an interface are public, static & final by default, and methods are public and abstract. For example
public interface MyInterface { public String hello = "Hello"; public void sayHello(); } // A class that implements interface. class testClass implements MyInterface { // Implementing the capabilities of interface. public void sayHello() { System.out.println("Hello Interface"); } }
The interface example above contains one variable and one method. The variable can be accessed directly from the interface, like this:
System.out.println(MyInterface.hello);
Accessing a variable from an interface is very similar to accessing a static variable (by default variable are static in interface) in a class. The method, however, needs to be implemented by some class before you can access it. A Java library example is, Comparator Interface. If a class implements this interface, then it can be used to sort a collection.
Use of interface
They are used for full abstraction. Since methods in interfaces do not have body, they have to be implemented by the class before you can access them. The class that implements interface must implement all the methods of that interface. Also, java does not allow you to extend more than one class, However you can implement more than one interfaces in your class.
Implementing Multiple Interfaces
A Java class can implement multiple Java interfaces. In that case the class must implement all the methods declared in all the interfaces implemented. Here is an example:
// Class implementing mutiple interface public class MyInterfaceImpl implements MyInterface, MyOtherInterface { public void sayHello() { System.out.println("Hello"); } public void sayGoodbye() { System.out.println("Goodbye"); } }
If the interfaces are not located in the same packages as the implementing class, you will also need to import the interfaces. Java interfaces are imported using the import instruction just like Java classes.
Key points about Interfaces
- We can’t instantiate an interface. Interface provides full abstraction as none of its methods have body. From JDK 8, we can now add default implementation for interface methods. This default implementation has special use and does not affect the intention behind interfaces.
- Class that implements any interface must implement all the methods of that interface, else the class should be declared abstract.
- Another feature that was added in JDK 8 is that we can now define static methods in interfaces which can be called independently without an object. Note: these methods are not inherited.
- All the interface methods are by default abstract and public. Variables declared in interface are public, static and final by default. Interface variables must be initialized at the time of declaration .
- An interface can extend any interface but cannot implement it. Class implements interface and interface extends interface.