public class ShapeTest {
public static void main(String[] args) {
Circle c = new Circle("c1");
Rectangle r = new Rectangle("r1");
// the following is illegal
// Shape s = new Shape();
c.draw();
r.draw();
}
}
abstract class Shape {
protected String name;
public Shape(String n) {
name = n;
}
abstract public void draw();
abstract public void erase();
}
class Circle extends Shape {
public Circle(String n) {
super(n);
}
public void draw() {
System.out.println(name + ": Circle.draw()");
}
public void erase() {
System.out.println(name + ": Circle.erase()");
}
}
class Rectangle extends Shape {
public Rectangle(String n) {
super(n);
}
public void draw() {
System.out.println(name + ": Rectangle.draw()");
}
public void erase() {
System.out.println(name + ": Rectangle.erase()");
}
}
ShapeTest.java output
c1: Circle.draw()
r1: Rectangle.draw()
Tag: Study Code Program Java
No comments:
Post a Comment