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.

The following example illustrates a class implements an interface and provides detailed implementation for the interface's methods:

interface Vehicle {
    void start();
    void stop();
}

class Car implements Vehicle {
    void start() {
        // starts the engine
    }
    
    void stop() {
        // stop the engine    
    }
}

Unlike extends keyword, a class can implement multiple interfaces. The interface names are separated by commas. For example:

interface Peson { }
interface Employee {}
class Director implements Person, Employee { }    

If the implemented class does not provide implementation for the interface's methods, the class must be abstract. For example:

interface Animal {
    void eat();
}

abstract class Reptile implements Animal {
    abstract void crawl();
}

In that case, the first non-abstract class must implement the method. For example:

class Crocodile extends Reptile {
    void eat() {
        // eats
    }
    
    void crawl() {
        // crawls
    }
}

 

Related keyword: classinterface, abstract and extends. See all keywords in Java.

 

Related Topics:

 

Other Recommended Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment

   


Comments 

#2BANJA DENIS VICTOR2017-05-09 10:06
what of this
Public class extends Frame implements Mouse{
}
Does it mean that we have inherited a class Frame and an interface Mouse
Quote
#1aashish choudhary2015-05-21 00:52
i want to learn java for the placement point of view. as of now i just wants to focus on such technical questions which generelly asked in interviews.
Quote