0% found this document useful (0 votes)
14 views4 pages

List Programs 5 and 6

This document contains code for two Java programs that demonstrate polymorphism and abstract classes/methods. The first program creates a base Shape class with subclasses Circle, Triangle, and Square. Each subclass overrides the draw() and erase() methods to demonstrate polymorphism when an object of each type calls the methods. The second program creates an abstract Shape class with abstract calculateArea() and calculatePerimeter() methods. Concrete Circle1 and Triangle1 subclasses implement these methods to calculate the area and perimeter for each shape type. The main method creates objects of each subclass and calls the methods.

Uploaded by

madhugreddy456
Copyright
© © All Rights Reserved
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% found this document useful (0 votes)
14 views4 pages

List Programs 5 and 6

This document contains code for two Java programs that demonstrate polymorphism and abstract classes/methods. The first program creates a base Shape class with subclasses Circle, Triangle, and Square. Each subclass overrides the draw() and erase() methods to demonstrate polymorphism when an object of each type calls the methods. The second program creates an abstract Shape class with abstract calculateArea() and calculatePerimeter() methods. Concrete Circle1 and Triangle1 subclasses implement these methods to calculate the area and perimeter for each shape type. The main method creates objects of each subclass and calls the methods.

Uploaded by

madhugreddy456
Copyright
© © All Rights Reserved
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/ 4

/*List 5: Develop a JAVA program to create a class named

shape. Create three sub classes namely: circle, triangle and


square, each class has two member functions named draw () and
erase (). Demonstrate polymorphism concepts by developing
suitable methods, defining member data and main program.*/
class shape{ //base class
void draw()
{
System.out.println("Drawing a General Shape");
}
void erase()
{
System.out.println("Erasing a General Shape");
}
}
class Circle extends shape{
void draw()
{
System.out.println("Drawing a Circle");
}
void erase()
{
System.out.println("Erasing a Circle");
}
}
class Triangle extends shape{
void draw()
{
System.out.println("Drawing a Triangle");
}
void erase()
{
System.out.println("Erasing a Triangle");
}
}
class Square extends shape{
void draw()
{
System.out.println("Drawing a Square");
}
void erase()
{
System.out.println("Erasing a Square");
}
}
class Poly{

public static void main(String args[]){


shape c1=new Circle();
shape t1=new Triangle();
shape s1=new Square();
System.out.println("Methods called using respective objects");
c1.draw();
c1.erase();
t1.draw();
t1.erase();
s1.draw();
s1.erase();
System.out.println("Demonstration of Polymorphism");
displayShape(c1);
displayShape(t1);
displayShape(s1);
}
static void displayShape(shape sh) {
sh.draw();
sh.erase();
}
}
/*List 6: Develop a JAVA program to create an abstract class
Shape with abstract methods calculateArea() and
calculatePerimeter(). Create subclasses Circle and Triangle that
extend the Shape class and implement the respective methods to
calculate the area and perimeter of each shape.*/
abstract class abShape{
public abstract void calculateArea();
public abstract void calculatePerimeter();
}
class Circle1 extends abShape {
double area,radius,perimeter;
public Circle1(double radius) {
this.radius=radius;
}
public void calculateArea() {
area=Math.PI*radius*radius;
System.out.println("Area of the Circle=> "+area);
}
public void calculatePerimeter() {
perimeter=2*Math.PI*radius;
System.out.println("Perimeter of the Circle=> "+perimeter);
}
}
class Triangle1 extends abShape{
double area,perimeter,side1,side2,side3,s;
public Triangle1(double side1,double side2,double side3) {
this.side1=side1;
this.side2=side2;
this.side3=side3;
}

public void calculateArea() {


// Heron's formula used to calculate the area of a triangle
s = (side1 + side2 + side3) / 2;
area= Math.sqrt(s * (s - side1) * (s - side2) * (s -
side3));
System.out.println("Area of the Triangle=> "+area);
}
public void calculatePerimeter() {
perimeter=side1+side2+side3;
System.out.println("Perimeter of the Triangle=>
"+perimeter);
}
}
class mainMethod{
public static void main (String args[]) {
Circle1 c1=new Circle1(6.0);
Triangle1 t1=new Triangle1(4.0,6.0,8.0);
c1.calculateArea();
c1.calculatePerimeter();
t1.calculateArea();
t1.calculatePerimeter();
}
}

You might also like