Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "implements" do on a class?

Tags:

java

php

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

like image 767
Webnet Avatar asked Dec 29 '10 21:12

Webnet


People also ask

What does implementing a class do?

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

What are implements used?

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.

What does implements mean Java?

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.

What does implements mean in programming?

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.


1 Answers

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.

like image 143
Wayne Hartman Avatar answered Oct 04 '22 20:10

Wayne Hartman