Inheritance and Polymorphism Example - Shapes of Different Types


Shapes.java:

// class Shape
//
// base class for shapes to demonstrate polymorphism
// field:
//    name
// methods:
//    toString
//    area (always returns 0)
//
public class Shape {
	protected String name;

	// constructors
	public Shape() {
		name = "default";
	}

	public Shape(String name) {
		this.name = name;
	}

	public String toString() {
		return "shape " + name;
	}

	// since Shape isn't really a shape, the area is always 0
	public double area() {
		return 0;
	}
}

Rectangle.java:

// class Rectangle, subclass of Shape
//
// fields:
//    length and width (doubles)
// methods:
//    toString
//    area (length * width)
//
public class Rectangle extends Shape {
	private double length;
	private double width;

	// constructors
	public Rectangle() {
		length = width = 1;
	}

	public Rectangle(String nn, double len, double wid) {
		name = nn;
		length = len;
		width = wid;
	}

	public String toString() {
		return "Rectangle " + name + ": " + length + " by " + width;
	}

	// calculate and return area
	public double area() {
		return length * width;
	}
}

Circle.java:

// class Circle, subclass of Shape
//
// field:
//    radius (double)
// methods:
//    toString
//    area (pi * r * r)
//
public class Circle extends Shape {
	private double radius;

	// constructors
	public Circle() {
		radius = 1;
	}

	public Circle(String name, double rad) {
		this.name = name;
		radius = rad;
	}

	public String toString() {
		return "Circle " + name + ": " + radius;
	}

	// calculate and return area
	public double area() {
		return Math.PI * radius * radius;
	}
}

UseShapes.java:

// program to demonstrate polymorphism under inheritance
//
// Rectangle and Circle are subclasses of Shape so they can
// be used anywhere a Shape can be used

public class UseShapes {
	public static void main(String[] args) {
		int i;
		Shape[] shapearr = new Shape[5];

		// array elements must refer to Shape objects
		// subclasses of Shape are valid, so array can contain Circle
		// and Rectangle objects
		shapearr[0] = new Shape();
		shapearr[1] = new Rectangle();
		shapearr[2] = new Circle();
		shapearr[3] = new Rectangle("myrect", 3,5);
		shapearr[4] = new Circle("mycircle", 4);

		for (i = 0 ; i < shapearr.length ; i++)
			printShape(shapearr[i]);

		Circle circle = new Circle("newcirc", 2);
		System.out.println();
		printShape(circle);
	}

// print a Shape object
// any Shape can be passed as the actual parm: a Shape, a Circle, or a Rectangle
//
// the class of the actual object passed (not the class of the reference)
// is used to choose which version of area to call
	public static void printShape(Shape aShape) {
		System.out.println(aShape);
		System.out.println("   area " + aShape.area());
	}
}

Output:

shape default
   area 0.0
Rectangle default: 1.0 by 1.0
   area 1.0
Circle default: 1.0
   area 3.141592653589793
Rectangle myrect: 3.0 by 5.0
   area 15.0
Circle mycircle: 4.0
   area 50.26548245743669

Circle newcirc: 2.0
   area 12.566370614359172


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2017