Example illustrating inheritance and abstract classes

***********************************
# Example illustrating inheritance and abstract classes.

    * Shape.java The parent class (abstract) for all closed, open, curved, and straight-edged shapes.
    * Curve.java An (abstract) curved Shape (open or closed).
    * StraightEdgedShape.java A Shape with straight edges (open or closed).
    * Measurable.java Interface defining classes with measurable areas.
    * Circle.java A circle that extends Shape and implements Measurable.
    * MeasureUtil.java Operates on Measurable instances.
    * Polygon.java A closed Shape with straight edges; extends StraightEdgedShape and implements Measurable.
    * Rectangle.java A rectangle that satisfies the Measurable interface; extends Polygon.
    * MeasureTest.java Driver for example.
**************************************
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Shape.java 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/** The parent class for all closed, open, curved, and 
 *  straight-edged shapes.
 *
 ############################
public abstract class Shape {
  protected int x, y;

  public int getX() {
    return(x);
  }

  public void setX(int x) {
    this.x = x;
  }

  public int getY() {
    return(y);
  }

  public void setY(int y) {
    this.y = y;
  }
}
#############################
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Curve.java An (abstract) curved Shape (open or closed)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/** A curved shape (open or closed). Subclasses will include
 *  arcs and circles.
 *
***********************

public abstract class Curve extends Shape {}
##############################
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
StraightEdgedShape.java A Shape with straight edges (open or closed). 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/** A Shape with straight edges (open or closed). Subclasses
 *  will include Line, LineSegment, LinkedLineSegments,
 *  and Polygon.
 *
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public abstract class StraightEdgedShape extends Shape {}
################################
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Measurable.java Interface defining classes with measurable areas
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/** Used in classes with measurable areas. 
 *
 **************

public interface Measurable {
  double getArea();
}
#################################
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Circle.java A circle that extends Shape and implements Measurable.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/** A circle. Since you can calculate the area of
 *  circles, class implements the Measurable interface.
 *
***********************************
public class Circle extends Curve implements Measurable {
  private double radius;

  public Circle(int x, int y, double radius) {
    setX(x);
    setY(y);
    setRadius(radius);
  }

  public double getRadius() {
    return(radius);
  }

  public void setRadius(double radius) {
    this.radius = radius;
  }

  /** Required for Measurable interface. */

  public double getArea() {
    return(Math.PI * radius * radius);
  }
}
############################
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
MeasureUtil.java Operates on Measurable instances
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/** Some operations on Measurable instances. 
 *

************************
public class MeasureUtil {
  public static double maxArea(Measurable m1,
                               Measurable m2) {
    return(Math.max(m1.getArea(), m2.getArea()));
  }

  public static double totalArea(Measurable[] mArray) {
    double total = 0;
    for(int i=0; i
~~~~~~~~~~~~~~~~~~~~~~

Permanent link to this article: http://bangla.sitestree.com/example-illustrating-inheritance-and-abstract-classes-2/

Leave a Reply