Abstract Class
Excersise : 4
Create a program with abstract class
Program
import java.io.*;
import java.lang.*;
abstract class Figure
{
double dim1;
double dim2;
Figure(double A,double B)
{
dim1=A;
dim2=B;
}
abstract double area();
}
class Rectangle extends Figure
{
Rectangle(double x,double y)
{
super(x,y);
}
double area()
{
System.out.println("inside area for rectangle");
return dim1*dim2;
}
}
class Triangle extends Figure
{
Triangle(double x1,double y1)
{
super(x1,y1);
}
double area()
{
System.out.println("inside area for triangle");
return dim1*dim2/2;
}
}
class Abstractareas
{
public static void main(String arg[])throws IOException
{
Figure figref;
Rectangle r;
Triangle t;
DataInputStream din=new DataInputStream(System.in);
double x,y;
System.out.println("Enter the value of x:");
x=Double.parseDouble(din.readLine());
System.out. println("Enter the value of y:");
y=Double.parseDouble(din.readLine());
r=new Rectangle(x,y);
figref=r;
System.out.println("Area is"+figref.area());
double x1,y1;
System.out.println("Enter the value of x1:");
x1=Double.parseDouble(din.readLine());
System.out. println("Enter the value of y1:");
y1=Double.parseDouble(din.readLine());
t=new Triangle(x1,y1);
figref=t;
System.out.println("Area is"+figref.area());
}
}
Output
Post a Comment
Thanks for your comments. Please be visit and given positive review on our site.