All methods in the interface are implicitly public and abstract. To understand the use of interface in Java better, let see an Java interface example. Each having its unique interface implementation in Java method to play music. Which play method should it inherit? This may cause serious design issues. And hence, Java does not allow multiple inheritance. But you cannot extend two classes in Java.
An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final. The interface keyword is used to declare an interface. An interface is implicitly abstract. You do not need to use the abstract keyword while declaring an interface. When a class implements an interface, you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface. If a class does not perform all the behaviors of the interface, the class must declare itself as abstract.
A class uses the implements keyword to implement an interface. The implements keyword appears in the class declaration following the extends portion of the declaration. Checked exceptions should not be declared on implementation methods other than the ones declared by the interface method or subclasses of those declared by the interface method.
However interfaces have no implementation so that's not possible. An interface can however extend another interface, which means it can add more methods and inherit its type.
Ben Souther. I like Mayu Mayooresan. This is my understanding An interface in Java is a blueprint of a class. It has static constants and abstract methods. The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java.
In other words, you can say that interfaces can have abstract methods and variables. It cannot have a method body. An interface is declared by using the interface keyword. It provides total abstraction; means all the methods in an interface are declared with the empty body, and all the fields are public, static and final by default.
A class that implements an interface must implement all the methods declared in the interface. Since Java 8 , interface can have default and static methods which is discussed later. In other words, Interface fields are public, static and final by default, and the methods are public and abstract.
As shown in the figure given below, a class extends another class, an interface extends another interface, but a class implements an interface. In this example, the Printable interface has only one method, and its implementation is provided in the A6 class. 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. 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.
0コメント