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...
The implements keyword is used to implement an interface . The interface keyword is used to declare a special type of class that only contains abstract methods. To access the interface methods, the interface must be "implemented" (kinda like inherited) by another class with the implements keyword (instead of extends ).
Apply the noun implement when you want to use a fancy word for "tool." A knife and fork are implements for handling food. The noun implement is a very useful word for just about anything you want to describe as a tool or a thing that helps you do something.
In Java, the implements keyword is used to make a class adheres to contract defined by an interface. The implemented class must provide concrete implementation for the methods defined by the interface. If not, the class must be abstract.
In IT, the word implementation usually refers to installing a new hardware or software system or application. It can also mean the inclusion of a specific technical specification, software component or software standard. For example, software development tools contain implementations of a programming language.
Implements means that it takes on the designated behavior that the interface specifies. Consider the following interface:
public interface ISpeak { public String talk(); } public class Dog implements ISpeak { public String talk() { return "bark!"; } } public class Cat implements ISpeak { public String talk() { return "meow!"; } }
Both the Cat
and Dog
class implement the ISpeak
interface.
What's great about interfaces is that we can now refer to instances of this class through the ISpeak
interface. Consider the following example:
Dog dog = new Dog(); Cat cat = new Cat(); List<ISpeak> animalsThatTalk = new ArrayList<ISpeak>(); animalsThatTalk.add(dog); animalsThatTalk.add(cat); for (ISpeak ispeak : animalsThatTalk) { System.out.println(ispeak.talk()); }
The output for this loop would be:
bark!
meow!
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 Comparable
. If your object implements this interface, you can write an implementation that consumers can use to sort your objects.
For example:
public class Person implements Comparable<Person> { private String firstName; private String lastName; // Getters/Setters public int compareTo(Person p) { return this.lastName.compareTo(p.getLastName()); } }
Now consider this code:
// Some code in other class List<Person> people = getPeopleList(); Collections.sort(people);
What this code did was provide a natural ordering to the Person
class. Because we implemented the Comparable
interface, we were able to leverage the Collections.sort()
method to sort our List
of Person
objects by its natural ordering, in this case, by last name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With