Ask Question

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.

+4
Answers (1)
  1. 30 December, 00:45
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers