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

Exp 5

Experiment 5

Uploaded by

lasele3905
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Exp 5

Experiment 5

Uploaded by

lasele3905
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

// 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
{
protected String name;

public Shape(String name) {


this.name = name;
}

public void draw() {


System.out.println("Drawing a " + name);
}

public void erase() {


System.out.println("Erasing a " + name);
}
}

class Circle extends Shape {


private double radius;

public Circle(String name, double radius) {


super(name);
this.radius = radius;
}

@Override
public void draw() {
System.out.println("Drawing a circle with radius " + radius);
}

@Override
public void erase() {
System.out.println("Erasing a circle with radius " + radius);
}
}

class Triangle extends Shape {


private double base;
private double height;

public Triangle(String name, double base, double height) {


super(name);
this.base = base;
this.height = height;
}

@Override
public void draw() {
System.out.println("Drawing a triangle with base " + base + " and height "
+ height);
}

@Override
public void erase() {
System.out.println("Erasing a triangle with base " + base + " and height "
+ height);
}
}

class Square extends Shape {


private double side;

public Square(String name, double side) {


super(name);
this.side = side;
}

@Override
public void draw() {
System.out.println("Drawing a square with side length " + side);
}

@Override
public void erase() {
System.out.println("Erasing a square with side length " + side);
}
}

public class ShapeDemo {


public static void main(String[] args) {
Shape[] shapes = new Shape[3];

shapes[0] = new Circle("Circle", 5.0);


shapes[1] = new Triangle("Triangle", 4.0, 6.0);
shapes[2] = new Square("Square", 3.0);

for (Shape shape : shapes) {


shape.draw();
shape.erase();
System.out.println();
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy