Calculate the area of rectangle and triangle using interfaces

The following program is used to calculate the area of rectangle and triangle using interfaces. The program uses Java for the implementation of multiple inheritance.

An interface defines a set of methods that will be implemented by a class. Syntactically, interfaces are similar to abstract classes, except that no method can include a body. This means that an interface provides no implementation whatsoever of the method it defines.

Also read: 12 Best FREE Online Courses with Certificates 2020 [Updated]

Once an interface is defined, any number of classes can implement it. This makes it possible for two or more classes to provide the same functionality even though they might do so in different ways. Also, one class can implement any number of interfaces.

Program: 

package sss;
interface compute
{
double calculate();
}
class Rectangle implements compute
{
double l,b;
void getValues()
{
l=10.5F
b=7.3F
}
public double calculate()
{
return (l*b);
}
}
class TRI extends Rectangle implements compute
{
public double calculate()
}
return (0.5*b*l);
}
}
class prog6b
{
public static void main(String[] args)
{
Rectangle R = new Rectangle();
R.getvalues();
System.out.println("Area of Rectangle =" +R.calculate());
TRI T = new TRI();
T.getvalues();
System.out.println("Area of Triangle =" +T.calculate());
}
}

The output of program to calculate the area of rectangle and triangle using interfaces is shown below.

Output:

Area of Reactangle = 76.65000200271606
Area of Triangle = 38.32500100135803

Also read: Best websites to Learn Coding Online

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
even and odd numbers using assembly language

To Separate Even and Odd Numbers using Assembly Language

Next Post
pliers for saltwater fishing

8 Best Pliers For Saltwater Fishing in 2022

Popup subscribe Form Tiny Quip

Connect

Some Catchy Line to Grab your Attention

Get Our Prime Content Delivered to your Inbox for Free!! Join the Ever-Growing Community… Right Now.

Total
0
Share