1

I have read the replies from those post(Why are interfaces useful?) and (Why use an interface when the class can directly implement the functions?), which is the similar question as my this post. But I still cannot understand it why we use interface since we need to implement the method in our derived class?

I can understand that why we use 'extends', which is very good for code reuse since we do not need to write the method implementation if we extend a class and we do not want to override it. But for implement an interface, we need to write the abstract function in the interface, in the class which implements, we need to write the implementations details. I do not think this does any benefit. Please explain why we even need the interface. Thanks.

closed as unclear what you're asking by Telastyn, Jörg W Mittag, user22815, gnat, amon Nov 13 '17 at 21:19

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • What, precisely don't you understand in the answers to those two questions? What do you understand in those answers? What do you understand about interfaces? Did you read Parnas's seminal paper on Modularity? – Jörg W Mittag Nov 12 '17 at 1:27
  • @JörgWMittag Yes, I read the answers, but they seem they did not directly answer the question of mine. Could you please answer the question in my second paragraph? I understand the 'extend' a class but did not understand why 'implement' an interface. – FullStackDeveloper Nov 12 '17 at 1:32
  • @EntryLeveDeveloper also see: Polymorphism. There is a lot of information out there on the topic, and your question would benefit from more details about what, exactly, you do not understand. As it is right now, it sounds like "I do not understand the topic at all and want someone to explain it to me, even though there are already explanations out there." Please take some time to read up and come back with a more specific question. – user22815 Nov 12 '17 at 2:07
  • I would also recommend reading this answer to one of the questions you linked, which has a good explanation on how interfaces work and why you would want to use them. – user22815 Nov 12 '17 at 2:09
  • @Snowman My specific question is in my second paragraph, hope it gives what you asked. "But for implement an interface, we need to write the abstract function in the interface, in the class which implements, we need to write the implementations details." Extend a class can let us code reuse, but implement an interface did not let us code reuse. – FullStackDeveloper Nov 12 '17 at 2:39
6

If I understand you correctly, your question is the following: since we use interfaces for code reusability, why they only have abstract methods? It makes no sense.

The answer is simple: interfaces are not for code reusability.


So why do we use them?

Suppose we have three classes: Dog, Chicken and Fish. All of these extend the class Animal. However, while Dog and Chicken can run, a Fish can only swim.

class Dog extends Animal
{
    void Run()
    {
        // how a dog runs
    }
}
class Chicken extends Animal
{
    void Run()
    {
        // how a chicken runs
    }
}
class Fish extends Animal
{
    void Swim()
    {
        // how a fish swims
    }
}

Now I will ask you: is this code valid?

MakeAnimalRun(Animal animal)
{
    animal.Run();
}

No, it isn't. While it is perfectly logical to invoke the Run() method for a Dog or a Chicken, it doesn't make any sense to invoke it for a Fish, since it can only swim.

To be able to call the animal's Run() method, I need to make sure the animal can actually run.

That is where interfaces become handy. Interfaces are useful when we need to know for sure that an object has a specific behavior.

So let us create an interface called ICanRun, and make all the animals that can run implement it:

interface ICanRun
{
    void Run();
}

class Dog extends Animal implements ICanRun
{
    void Run()
    {
        // how a dog runs.
    }
}

class Chicken extends Animal implements ICanRun
{
    void Run()
    {
        // how a chicken runs.
    }
}

And now this code is perfectly valid since we know for sure that the animal can run:

void MakeAnimalRun(ICanRun animal)
{
    animal.Run();
}

Notice that when I talk about a behavior, I only talk about the ability to do something, and not about how this ability is actually expressed. Both a Dog and a Chicken can run, but they run very differently: a Dog uses 4 legs while a chicken uses only 2. So the implementation of the Run() method for the Dog will be very different from the implementation for the Chicken. When I am defining this interface, I am concerned about "what can that animal do" and not about "how does it do it".


To summarize:

Interfaces are not for code reusability. Interfaces represent a behavior that an object has and do not care about how it implements this behavior. This allows us to know for sure that the object we deal with has the behavior we are looking for.

  • @EntryLeveDeveloper You are welcome. By the way, you could also implement ICanRun on a human being, which does not extend Animal. That is the point. Some animals have a common behavior, but not only animals. That is when we use interfaces. If you need any clarification - feel free to comment. – Sipo Nov 12 '17 at 16:30
7

Interfaces exist for one reason, and one reason only: to allow different implementations to be created that are all compatible with the interface.

This interface

Accepts these implementations

More specifically, the appliance at the end of those cords can be anything that satisfies the interface contract, which is 120 volts AC at 60 Hertz, drawing less than 12 amps over 14 gauge wire.

The wall socket neither knows nor cares what is at the end of that cord, so long as it meets the socket's electrical specifications.

  • Thanks. I think I understand what you explained here. Could you please explain this for me? Thanks a lot. "But for implement an interface, we need to write the abstract function in the interface, in the class which implements, we need to write the implementations details." Extend a class can let us code reuse, but implement an interface did not let us code reuse. – FullStackDeveloper Nov 12 '17 at 2:41
  • I don't really understand what that sentence means. Absent any additional context, I think you can safely ignore it. – Robert Harvey Nov 12 '17 at 2:44
  • If we have an interface IFoo, and we have a class MyFoo to implement. In class MyFoo, we need to write the implementation detail for the methods. In this case, interface IFoo did not do any benefits for us (It do not have the code reuse). Why do we need to have interface here? – FullStackDeveloper Nov 12 '17 at 2:48
  • You might not need one. Depends on whether or not you need to provide more than one implementation. – Robert Harvey Nov 12 '17 at 3:00
  • After adding what kinds of requirements, you will start using the interface based on my IFoo and MyFoo? – FullStackDeveloper Nov 12 '17 at 3:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.