implements keyword in java

The ” implements ” keyword is used to make a class to follow the contract defined by an interface.

The implemented class should provide concrete implementation for the methods declared in the interface.
If not, the class should be abstract.

Example
  1. interface Shape{
  2.  
  3. public double getArea();
  4.  
  5. }
  6.  
  7. Class Circle implements Shape{
  8.  
  9. double radius=5;
  10.  
  11. public double getArea(){
  12.  
  13.         return 3.142*radius*radius;
  14.     }
  15. }
  16.  
  17. Class Triangle implements Shape{
  18.  
  19. double base=5;
  20. double height=10;
  21.  
  22. public double getArea(){
  23.  
  24.               return 0.5*base*height;
  25.     }
  26. }
interface Shape{

public double getArea();

}

Class Circle implements Shape{

double radius=5;

public double getArea(){

        return 3.142*radius*radius;
    }
}

Class Triangle implements Shape{

double base=5;
double height=10;

public double getArea(){

              return 0.5*base*height;
    }
}


Unlike class can extend only one class, a class can implement multiple interfaces.The interface names are separated by commas.

Example
  1. interface Polygon{
  2.  
  3.        public int getNumberOfSides();
  4.  
  5. }
  6.  
  7. Class Triangle implements Shape,Polygon{
  8.  
  9. double base=5;
  10. double height=10;
  11.  
  12. public double getArea(){
  13.        return 0.5*base*height;
  14. }
  15.  
  16. public int getNumberOfSides(){
  17.  
  18.        return 3;
  19.      }
  20. }
interface Polygon{

       public int getNumberOfSides();
 
}
  
Class Triangle implements Shape,Polygon{

double base=5;
double height=10;

public double getArea(){
       return 0.5*base*height;
}

public int getNumberOfSides(){

       return 3;
     }
}


If the implemented class does not provide implementation for any of the interface’s methods, then such class must become abstract class.

Example
  1. interface Vehicle {
  2.     public void drive();
  3. }
  4.  
  5. abstract class Car implements Vehicle {
  6.     abstract void speed();
  7. }
interface Vehicle {
    public void drive();
}

abstract class Car implements Vehicle {
    abstract void speed();
}

In the above case, class “Car” is implementing Vehicle interface but its not providing the definition for drive() method and hence “Car” class should become abstract.

Now Any class which provide that implementation will become concrete class as below

  1. class BenzCar extends Car{
  2.  
  3.  public void drive(){
  4.  
  5.          System.out.println("Driving Benz car");
  6.      }
  7. public void speed()
  8.     {
  9.         System.out.println("Max: 350Kmph");
  10.     }
  11. }
class BenzCar extends Car{

 public void drive(){

         System.out.println("Driving Benz car");
     }
public void speed()
	{
		System.out.println("Max: 350Kmph");
	}
}


Since “BenzCar” class provides definition for all the abstract methods inherited from its parents, It becomes concrete class.

Note:

We can instantiate only Concrete class,We cant create object for abstract class.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on