Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OOP the point of interface [duplicate]

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 instanceOf 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

like image 375
luigi7up Avatar asked May 13 '11 12:05

luigi7up


People also ask

What is the purpose of interface in OOP?

Interfaces allow you to specify what methods a class should implement. Interfaces make it easy to use a variety of different classes in the same way. When one or more classes use the same interface, it is referred to as "polymorphism".

What happens when two interfaces have same method?

However, if two interfaces implement the same default method, then there is a conflict. In cases where one interface inherits another interface and both of them implement a default method, an implementing class would use the default method of the child interface.

What is the advantage of using interface in Java?

Why do we use an Interface? It is used to achieve total abstraction. Since java does not support multiple inheritances in the case of class, by using an interface it can achieve multiple inheritances. It is also used to achieve loose coupling.

What does it mean for an interface to extend another interface?

An interface extends another interface because the interface that extends another interface just adds its own abstract method(s) and doesn't provide method definitions for abstract methods of the other interface. An interface will never extend a class as interfaces don't provide method definitions.


1 Answers

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).

like image 163
Brian Driscoll Avatar answered Oct 22 '22 11:10

Brian Driscoll