Skip to main content

OOP the point of interface



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


Source: Tips4allCCNA FINAL EXAM

Comments

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

    ReplyDelete
  2. 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:


    force a class to implement some methods
    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)

    ReplyDelete
  3. "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();
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex