An interface is a completely " abstract class " that is used to group related methods with empty bodies:.
To access the interface methods, the interface must be "implemented" kinda like inherited by another class with the implements keyword instead of extends. The body of the interface method is provided by the "implement" class:. However, it can be achieved with interfaces, because the class can implement multiple interfaces. Note: To implement multiple interfaces, separate them with a comma see example below.
In this example, the Drawable interface has only one method. Its implementation is provided by Rectangle and Circle classes. In a real scenario, an interface is defined by someone else, but its implementation is provided by different implementation providers. Moreover, it is used by someone else. The implementation part is hidden by the user who uses the interface.
Join Javatpoint Test Series
If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple inheritance. As we have explained in the inheritance chapter, multiple inheritance is not supported in the case of class because of ambiguity. However, it is supported in case of an interface because there is no ambiguity. It is because its implementation is provided by the implementation class.
- Recommended Posts:.
- Generating Social Capital: Civil Society and Institutions in Comparative Perspective;
- Real Estate Investments in Germany: Transactions and Development.
- How to declare an interface?.
- interface - Wiktionary.
- INTERFACE | meaning in the Cambridge English Dictionary.
For example:. As you can see in the above example, Printable and Showable interface have same methods but its implementation is provided by class TestTnterface1, so there is no ambiguity. Since Java 8, we can have method body in interface. But we need to make it default method. Let's see an example:. An interface which has no member is known as a marker or tagged interface, for example, Serializable, Cloneable, Remote, etc. Note: An interface can have another interface which is known as a nested interface.
Interface in Java
We will learn it in detail in the nested classes chapter. JavaTpoint offers too many high quality services.
Mail us on hr javatpoint. Please mail your requirement at hr javatpoint. Duration: 1 week to 2 week. Java Training Basics of Java. Abstract class Interface Abstract vs Interface.
Interface | Commercial Modular Carpet Tile