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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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;
    }
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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;
     }
}
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
2
3
4
5
6
7
interface Vehicle {
    public void drive();
}
 
abstract class Car implements Vehicle {
    abstract void speed();
}
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
2
3
4
5
6
7
8
9
10
11
class BenzCar extends Car{
 
 public void drive(){
 
         System.out.println("Driving Benz car");
     }
public void speed()
    {
        System.out.println("Max: 350Kmph");
    }
}
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

Karibasappa G C (KB)
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