209k views
3 votes
Write a program to create an interface Shape containing the method declarations of area and perimeter. Then, create a class Rectangle to implement the two methods.

1 Answer

0 votes

Answer:

Step-by-step explanation:

An interface is similar to a class, to create an interface we use interface keyword, in an interface there can be abstract methods and constants only,we can not instantiate an interface.All the variables declared inside an interface are public,static and final by default.

While using interface child class must use implements keyword

We can define an interface by using below syntax-

<interface> <interface-name>{

}

For example lets create Shpae interface

interface Shape{

public int area(int length, int width);

public int perimeter(int length, int width);

}

Now lets create Rectangle class-

class Rectangle implements Shape{

public int area(int length, int width){

int area = 0;

area = length * width;

return area;

}

public int perimeter(int length, int width);

int perimeter = 0;

perimeter = 2*(length+width);

return perimeter ;

}

User Ictus
by
5.6k points