0% found this document useful (0 votes)
10 views

Class Figure

The document contains Java code demonstrating the use of classes, inheritance, and abstract methods. It defines a base class 'Figure' with subclasses 'Rectangle' and 'Triangle' that override the area method to calculate their respective areas. Additionally, it showcases an abstract class 'A' with a concrete method and its subclass 'B' implementing the abstract method.

Uploaded by

deepthin1011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Class Figure

The document contains Java code demonstrating the use of classes, inheritance, and abstract methods. It defines a base class 'Figure' with subclasses 'Rectangle' and 'Triangle' that override the area method to calculate their respective areas. Additionally, it showcases an abstract class 'A' with a concrete method and its subclass 'B' implementing the abstract method.

Uploaded by

deepthin1011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

class Figure

double dim1;

double dim2;

Figure (double a, double b)

dim1 = a;

dim2 = b;

double area ()

System.out.println ("Area for Figure is undefined.");

return 0;

class Rectangle extends Figure

Rectangle (double a, double b)

super (a, b);

}
// override area for rectangle

double area ()

System.out.println ("Inside Area for Rectangle.");

return dim1 * dim2;

class Triangle extends Figure

Triangle (double a, double b)

super (a, b);

// override area for right triangle

double area ()

System.out.println ("Inside Area for Triangle.");

return dim1 * dim2 / 2;

class Main

public static void main (String args[])


{

Figure f = new Figure (10, 10);

Rectangle r = new Rectangle (9, 5);

Triangle t = new Triangle (10, 8);

Figure figref;

figref = r;

System.out.println ("Area is " + figref.area ());

figref = t;

System.out.println ("Area is " + figref.area ());

figref = f;

System.out.println ("Area is " + figref.area ());

Ppppppppppppppp

abstract class A

abstract void callme ();

// concrete methods are still allowed in abstract classes

void callmetoo ()

System.out.println ("This is a concrete method.");

class B extends A
{

void callme ()

System.out.println ("B's implementation of callme.");

class Main

public static void main (String args[])

B b = new B ();

b.callme ();

b.callmetoo ();

Ppppppppppppppppp

abstract class Figure

double dim1;

double dim2;

Figure (double a, double b)

{
dim1 = a;

dim2 = b;

// area is now an abstract method

abstract double area();

class Rectangle extends Figure

Rectangle (double a, double b)

super (a, b);

// override area for rectangle

double area ()

System.out.println ("Inside Area for Rectangle.");

return dim1 * dim2;

class Triangle extends Figure

Triangle (double a, double b)

super (a, b);


}

// override area for right triangle

double area ()

System.out.println ("Inside Area for Triangle.");

return dim1 * dim2 / 2;

class Main

public static void main (String args[])

{ // Figure f = new Figure(10, 10); // illegal now

Rectangle r = new Rectangle (9, 5);

Triangle t = new Triangle (10, 8);

Figure figref; // this is OK, no object is created

figref = r;

System.out.println ("Area is " + figref.area ());

figref = t;

System.out.println ("Area is " + figref.area ());

Pppppppppppppppppppppppp

You might also like