Ask Question
15 October, 19:56

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.

+2
Answers (1)
  1. 15 October, 21:49
    0
    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-

    {

    }

    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;

    }
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Write a program to create an interface Shape containing the method declarations of area and perimeter. Then, create a class Rectangle to ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers