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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.