The document defines an abstract Area class with an abstract computearea() method. It then defines three subclasses (Square, Circle, Rectangle) that extend Area and implement computearea() to calculate the area of each shape. The main method creates instances of each subclass and calls computearea() to output the area.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
13 views2 pages
Tutorial1 Group4 Solved 5102
The document defines an abstract Area class with an abstract computearea() method. It then defines three subclasses (Square, Circle, Rectangle) that extend Area and implement computearea() to calculate the area of each shape. The main method creates instances of each subclass and calls computearea() to output the area.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2
Q. Create an abstract class Area.
Create another 3 classes for computing Area of Square, Rectangle and Circle, and all of them will extend Area class. Use concept of Inheritance.
SOL:
//abstract class definition
abstract class Area { double a; double area; abstract void computearea(); }
class Square extends Area
{ void computearea() { a=5.5; //You can enter any value, or take input from user area=a*a; System.out.println("Area of Square is "+area+"sq units"); } }
class Circle extends Area
{ void computearea() { a=2.5; area=3.14*a*a; System.out.println("Area of Circle is "+area+"sq units"); } }
class Rectangle extends Area
{ double b; //for breadth void computearea() { a=4.0; b=2.0; area=a*b; System.out.println("Area of Rectangle is "+area+"sq units"); } }
public class Main
{ public static void main() { Square s=new Square(); s.computearea(); Circle c=new Circle(); c.computearea(); Rectangle r=new Rectangle(); r.computearea(); }