Because we can refer to the subclass object with the superclass reference variable, we can conduct the needed operation on this list. This is because, in Java, one class cannot extend more than one class. At this time, we can see how the interface is useful. As you can see, the interfaces allow us to define common behavior that can be implemented by any class, regardless of its inheritance.
Although the AdventureLoverFriend class extends the Friend class and the Facebook class extends the MobileApplication class, we can add common distractable behavior to them by implementing the Distractable interface.
Since Java allows us to refer implementation class objects with interface reference variable, we can write the following in the ExamPreparation class:.
Also, consider that we only focus on common distractable behavior in the printList method. We can show it like this in the code:. There are many other reasons why we need interfaces in Java. In this post, I tried to explain one of the more important concepts. Hope it was helpful! Published at DZone with permission of Vusala Hasanli. Ethical Hacking. Computer Graphics. Software Engineering. Web Technology.
Cyber Security. C Programming. Control System. Data Mining. Data Warehouse. Javatpoint Services JavaTpoint offers too many high quality services. Java Interface also represents the IS-A relationship. It cannot be instantiated just like the abstract class. Since Java 8, we can have default and static methods in an interface. Since Java 9, we can have private methods in an interface.
Why use Java interface? There are mainly three reasons to use interface. They are given below. It is used to achieve abstraction. By interface, we can support the functionality of multiple inheritance. It can be used to achieve loose coupling. How to declare an interface? Internal addition by the compiler The Java compiler adds public and abstract keywords before the interface method.
Moreover, it adds public, static and final keywords before data members. The relationship between classes and interfaces As shown in the figure given below, a class extends another class, an interface extends another interface, but a class implements an interface. Java Interface Example In this example, the Printable interface has only one method, and its implementation is provided in the A6 class.
File: TestInterface1. File: TestInterface2. Interface inheritance A class implements an interface, but one interface extends another interface. Let's see an example: File: TestInterfaceDefault. Let's see an example: File: TestInterfaceStatic. Improve this question. Nathan 2, 8 8 gold badges 26 26 silver badges 48 48 bronze badges. Vinoth Kumar Vinoth Kumar 2, 7 7 gold badges 22 22 silver badges 13 13 bronze badges.
Also a good explanation here. Add a comment. Active Oldest Votes. Improve this answer. I was also thinking about how interfaces are used. I hope this will help others: An interface is a contract or a protocol, or a common understanding of what the classes can do. Michal 31 6 6 bronze badges. Thein Thein 3, 2 2 gold badges 27 27 silver badges 32 32 bronze badges. Is the purpose of interface Comparable captured in your answer? Peter Tillemans Peter Tillemans 34k 9 9 gold badges 77 77 silver badges bronze badges.
You need them so you can type your objects outside the hierarchy. Amadan Amadan k 19 19 gold badges silver badges bronze badges. Some code won't compile without it. Iterable interface. Mihai Iorga Danny Crossley Danny Crossley 5. This is essentially the same as saying you need to use something because we have to. This does not adequately answer the question. I know that. I'm simply pointing out that we have to use them in this case, whether we like it or not.
I prefer to think of it as analogous to assembling a P. If this doesn't not adequately answer the question, I like to think at least it helps people understand why they are of use. Sign up or log in Sign up using Google. Sign up using Facebook.
0コメント