If a class implements another class... what does that mean? I found this code sample: http://www.java2s.com/Code/Php/Class/extendsandimplement.htm
But unfortunately it doesn't have any explanation with it...
|
If a class implements another class... what does that mean? I found this code sample: http://www.java2s.com/Code/Php/Class/extendsandimplement.htm But unfortunately it doesn't have any explanation with it... |
|||||
|
|
Implements means that it takes on the designated behavior that the interface specifies. Consider the following interface:
Both the What's great about interfaces is that we can now refer to instances of this class through the
The output for this loop would be:
Interface provide a means to interact with classes in a generic way based upon the things they do without exposing what the implementing classes are. One of the most common interfaces used in Java, for example, is For example:
Now consider this code:
What this code did was provide a natural ordering to the |
|||||
|
|
You should look into Java's interfaces. A quick Google search revealed this page, which looks pretty good. I like to think of an A simple example is the Implementing an interface requires two steps:
Interface Here's an example. Given this class
(Note: It's generally bad practice in Java to have public fields, but I am intending this to be a very simple plain-old-data type so I don't care about public fields!) If I want to be able to compare two Here's how the fleshed-out class might look:
You're probably thinking, what was the point of all this? The answer is when you look at methods like this: sorting algorithms that just expect "some kind of comparable object". (Note the requirement that all objects must implement NOTE: I'm using practices from Java 1.4 in this answer. |
||||
|
|
|
Interfaces are implemented through classes. They are purely abstract classes, if you will. In PHP when a class implements from an interface, the methods defined in that interface are to be strictly followed. When a class inherits from a parent class, method parameters may be altered. That is not the case for interfaces:
will cause an error, because the interface wasn't implemented as defined. Whereas:
Is allowed. |
|||
|
|
|
In Java a class can implement an interface. See http://en.wikipedia.org/wiki/Interface_(Java) for more details. Not sure about PHP. Hope this helps. |
|||
|
|
|
An interface defines a simple contract of methods all implementing classes must implement. When a class implements an interface, it must provide implementations for all its methods. I guess the poster assumes a certain level of knowledge about the language. |
|||
|
|
|
It is called an interface. Many OO languages have this feature. You might want to read through the php explanation here: http://de2.php.net/interface |
|||
|
|
|
An interface can be thought of as just a list of method definitions (without any body). If a class wants to implement and interface, it is entering into a contract, saying that it will provide an implementation for all of the methods listed in the interface. For more information, see http://download.oracle.com/javase/tutorial/java/concepts/ |
|||
|
|