Why is interface needed in java




















And we already made it inherit Messenger and added it to the Client class. But the Telegram class can easily implement both interfaces! Accordingly, we can give the Client class a Telegram object as a Messenger , and we can give it to the Smartphone class as an App.

In some places, it acts as an App. In other places, it acts as a Messenger. You've surely already noticed that interface methods are always 'empty', i. The reason for this is simple: the interface describes behavior, but it doesn't implement it. The specific way that fish, ducks, and horses swim is a question for the Fish , Duck , and Horse classes, not the interface. Just like changing the channel is a task for the TV.

The remote simply gives you a button for this. However, an interesting addition appeared in Java 8 — default methods. For example, your interface has 10 methods. Previously, before Java 8, interface methods had no implementation whatsoever: the compiler immediately gave an error.

We need to provide our own implementation for two other methods — eat and run — in all classes that implement CanSwim. We don't need to do this with the swim method: the implementation will be the same in every class. By the way, you've already come across interfaces in past tasks, even if you didn't notice : Here's a vivid example: You've worked with the List and Set interfaces!

The same diagram clearly gives an example where one class implements multiple interfaces at the same time. For example, LinkedList implements the List and Deque double-ended queue interfaces. You're familiar with the Map interface, or rather, with its HashMap implementation.

By the way, this diagram illustrates a feature: interfaces can be inherit other interfaces. This is necessary if you want to show the relationship between interfaces, where one interface is an extended version of another. Let's consider an example with the Queue interface. We haven't yet reviewed Queues , but it's rather simple and works like an ordinary queue, or line, at a store. You can only add items to the end of the queue, and can only take them from the beginning.

At some point, developers needed an enhanced version of the queue in order to add and take items at both ends. So they created a Deque interface, which is a double-ended queue. Abu Rashid February 22, at AM. Anonymous June 29, at AM. Unknown March 19, at AM. Pritish Patil October 19, at AM. Unknown July 25, at PM. Ramesh Reddy April 11, at AM. Unknown October 23, at AM. Anonymous January 28, at AM. Unknown December 23, at PM.

Unknown July 26, at AM. Unknown February 3, at AM. 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. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Collection Framework. Multi-threading in Java. Table of Contents. Save Article. Improve Article. Like Article. Previous Encapsulation in Java. Next Nested Interface in Java.

Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. Load Comments. What's New. Most popular in Java.



0コメント

  • 1000 / 1000