开发者

OOP the point of interface [duplicate]

开发者 https://www.devze.com 2023-03-05 22:30 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Interface vs Abstract Class (general OO)
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Interface vs Abstract Class (general OO)

EDIT: I just read the questions and answers to the questions from "possible duplicate" and I feel really sad that someone considers these two questions even similar... but, oh well...

-------------------------------------------------------------------------

Hello everyone, I am trying to understand something about Interfaces in OOP paradigm. I know the difference between abstract class and interface, I also know that interfaces basically allow easy multiple inheritance behaviour and design, but what I don't get is the "principle of promise". I mean, interface should be a promise that a class implementing an interface has all interface methods implemented.

What I don't understand is do we have to check if class implements interface with insta开发者_如何学GonceOf every time we call its methods? Without reading documentation you have no idea some class implements interface. And if you read the code than you can see yourself that there is that method defined and you can call it?!

If I have

case A.

class Ball{
function kick(){...};
}

or case B.

interface Kickable{
function kick;
}

class Ball implements Kickable{
function kick(){...};
}

the only difference is that in case A I'll get an error when calling a method that it doesn't exist ("in runtime") and in case B I'll get this error when trying to run the code while trying to "compile". Runtime and compile are definitely used wrong here (PHP environment).

I remember in Java there was a Runnable interface which enables threading. Why do we have to implement an interface Runnable and then define run() method in that class? I mean, class could have a Run method without implementing an interface and there are means to check if class has a special method defined. Ok, maybe my Java part of question is a bit confusing :)))

I'm sorry for such a confusing question, but I hope someone went through these problems in understanding and that now he can share his conclusion :)

Thanks, Luka


You've already named most of the benefits of interfaces in your question, namely:

  • they allow for multiple (interface) inheritance

You also mention that you know the difference between abstract classes and interfaces. Therein lies another benefit of using interfaces:

  • Any class can implement an interface, whereas not any class can derive from an abstract class

This is basically a re-hash of the first point above, but it puts it in a perspective that you might not have considered before. Take your Java Runnable example: If Runnable was an abstract class, then any and every class that implements threading would need to inherit from it. That would lead to extremely inflexible code in the end, as you'd not be able to inherit from any other base class. However, since Runnable is an interface, you can have any class implement it (regardless of what base class it may inherit from).

I understand your concern about having to check if a class implements an interface - unfortunately in a weakly typed language you will have to do that, especially since PHP type hinting hasn't totally come into its own yet.

In a strongly typed language, like Java, you generally don't have such concerns, as you will get a compile-time error if you call an interface method on a class that doesn't implement the interface (or doesn't implement the specific method).


No. You haven't to use instanceof. That's for run-time type checking. If you want to ensure that you are using a class that implements that interface simply put the interface type in your method signature. For example

public interface yourInterface{
      public void foo();
}

public class yourClass implements yourInterface{
     public void foo(){} //you need to implement this method, otherwise it won't compile
}

public class anotherClass{
     public void bar(yourInterface object){} //you can pass any object to "bar" method if the object implements yourInterface. yourClass object will be fine
}

Then some other nice things you can do, depends on your language. For example with java you can force a generic type to implement a given interface, allowing generic programming:

class generiClass<T extends yourInterface>{
      public void genericMethod(T object){} //you can use a generic T class, but T needs to implement yourInterface
}

The reason of interfaces are mainly 2:

  1. force a class to implement some methods
  2. Allow multiple inheritance like features in language without multiple inheritance (in language like C++, where you have multiple inheritance, you don't need an interface. Or saying it better, interfaces are quite the same thing of a pure abstract class)


"I also know that interfaces basically allow easy multiple inheritance behaviour and design"

I think you misunderstood that part. Interfaces allow you to ensure that a particular class has a set of properties/methods.

Example:

function foo($obj) {
  $obj->bar(); // fails with foo(array());
}

vs:

interface foobar {
  function bar();
}

function foo(foobar $obj) { // $obj *must* have a bar() method
  $obj->bar();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消