Ask Question
12 November, 18:37

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
Answers (1)
  1. 12 November, 19:59
    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?
Find 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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers