Why interfaces are needed




















That is why we need to describe these distractions in greater detail:. How can you achieve this? Well, you can think about inheritance. Because if we create a superclass named Distraction , and both the Facebook class and AdventureLoverFriend class extend it, we can collect all distractable things in one list.

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. To do this, we need to implement a method, i. To use an interface, some classes in our program must implement its methods. Let's invent a class whose objects 'can swim'.

The Duck class is 'associated' with the CanSwim interface by the implements keyword. You may recall that we used a similar mechanism to associate two classes through inheritance, but in that case we used the word extends. For complete clarity, we can translate ' public class Duck implements CanSwim ' literally as: 'The public Duck class implements the CanSwim interface'. This means that a class associated with an interface must implement all of its methods.

Note: our Duck class, just like the CanSwim interface, has a swim method, and it contains some logic. This is a mandatory requirement. If we just write public class Duck implements CanSwim without creating a swim method in the Duck class, the compiler will give us an error: Duck is not abstract and does not override abstract method swim in CanSwim Why?

Why does this happen? If we explain the error using the TV example, it would be like handing someone a TV remote control with a 'change channel' button that can't change channels. You could press the button as much as you like, but it won't work. The remote control doesn't change channels by itself: it only sends a signal to the TV, which implements the complex process of channel changing.

And so it is with our duck: it must know how to swim so it can be called using the CanSwim interface. If it doesn't know how, the CanSwim interface doesn't connect the two parties — the person and the program. The person won't be able to use the swim method to make a Duck swim inside the program.

Now you understand more clearly what interfaces are for. An interface describes the behavior that classes implementing the interface must have.

If we want to create several messengers, the easiest thing to do is to creating a Messenger interface. What does every messenger need? At a basic level, they must be able to receive and send messages. The compiler itself will 'force' us to implement them in our classes.

The most important of them is loose coupling. Imagine that we're designing a program that will collect client data. The Client class definitely needs a field to indicate which specific messenger the client is using. Thanks to interfaces you can achieve very low coupling between two classes. We're discussing this answer in chat. I dislike using the term "duck typing" because it means different things to different people.

We use pattern matching for LINQ because the C type system is too weak to capture the "monad pattern" we need; you'd need something like the Haskell type system. Eric: Doesn't "pattern matching" have the same problem? But I guess it's a broader idea than duck-typing. Daniel: Yeah, I suppose it does. Its six of one and half a dozen of the other I guess!

Add a comment. Interfaces are the best way to maintain well decoupled constructs. When writing tests, you will find that concrete classes will not work in your test environment. Sheldon Warkentin. Interfaces were invented to solve other problems, such as polymorphism. However, for me, they come into their own when implementing a Dependency Injection pattern.

How often you make uses of them and what makes you do so?? Never heard of polymorphy, you mean polymorphism? Pankaj Upadhyay: Multiple inheritance and interfaces are two different pairs of shoes. What if you need an interface of two unrelated classes with different behaviour? You can't solve that by multiple inheritance. And then you'll need something to describe the interface in order to provide polymorphic behaviour. Multiple inheritance is a blind alley to walk down in many cases, and it's too easy to shoot yourself in the foot sooner or later.

Lets simplify. If my interface implements two functions Display and Comment, and I have a class which implements them. Then why not I remove the interface and use the functions directly. What i am saying is that they are merely providing you the names of the functions that are needed to be implemented. If one can remember those functions then why create an interface — Pankaj Upadhyay. Pankaj, if that is all that you need the interface for, then don't use it. YOu use an interface when you have a program that wants to be ignorant of every aspect of the class and access it by its base Type, i.

They don't need to know any of the subclasses, just that it is of the type of the interface. As such, you can then call the implemented method of the sub-class through the reference to the object's interface.

This is basic inheritance and design stuff. Without it, you might as well use C. Even if you don't explicitly use it, the framework would not work without it. Show 10 more comments. That's certainly a concrete case speaking to the usefulness of an interface. Actually, foreach does not require IEnumerable: msdn.

I have studied all that but it's like holding the ear with other hand. If multiple inheritace was allowed, interface would have been a far choice. Interfaces ARE multiple inheritance, something that is often forgotten. However, they do not allow multiple inheritance of behavior and state. Mixins or traits allow multiple inheritance of behavior, but not shared state which causes issues: en.

Pankaj, offtopic, but do you mind if I ask what your native language is? I am from india. And here it is a common idiom which reflects doing easy things the difficult way. Daniel B. Jonathan Henson. Very well put! I'm guessing you'll like my article on abstract classes. Abstraction is everything. Let me try two approaches. And pardon me for the generalizations. Try 1 Say you're a native English speaker.

Do you ask: "Hey, were you born in the United States? Or do you ask: "Hey, do you speak English"? This is interface. The first Try is what gave my vote. I am totally using the American vs.

English Speaker analogy from now on. That is fantastic. Explained in a simpler way! There are 2 major reasons: Lack of multiple inheritance. You can inherit from one base class and implement any number of interfaces. That's the only way to "do" multiple inheritance in. COM interoperability. Anything that will need to be used by "older" technologies will need to have interfaces defined. Point 1 is definitely the reason and the reason developed by microsoft developers themselves — Pankaj Upadhyay.

Pankja Actually, they took the interface idea from Java like a good part of C 's features. Jesse C. So with the help of default implementation, we will give a default body for the newly added functions. Then the old codes will still work. Note: these methods are not inherited. A class can implement more than one interface. An interface can extends another interface or interfaces more than one interface. A class that implements interface must implements all the methods in interface.

All the methods are public and abstract. And all the fields are public, static, and final. It is used to achieve multiple inheritance. It is used to achieve loose coupling. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Skip to content. Change Language. Related Articles. Basics of Java. Operators in Java.



0コメント

  • 1000 / 1000