Polymorphism vs. Inheritance
What is Polymorphism? Examples of Polymorphism
Difference between Polymorphism and Inheritance
What can Polymorphism do?
Home
   Example of polymorphism:

Cylinder � Circle � Point � Shape

getArea getVolume getName
Shape 0.0 0.0 Abstract
Point 0.0 0.0 �Point�
Circle ?r2 0.0 �Circle�
Cylinder 2?r2 + 2 ?rh ?r2h �Cylinder�

public abstract class Shape extends Object
{
public double getArea()
{
  return 0.0
}
public double getVolume()
{
  return 0.0
}
public abstract String getName();
}
public class Point extends Shape
{
//override abstract method getName to return �Point�
public String getName()
{
  return �Point�;
}
public String toString()
{
  return getX() �+� getY();
}
}
public class Circle extends Point
{
private double radius;
�constructor, set/get radius, diameter, etc..
//override getArea
public double getArea()
{
  return Math.PI * getRadius()*getRadius;
}
//override getName
public String getName()
{
  return �Circle�
}
}

public class Cylinder extends Circle
{
private double height;
�constructor, set/get values�
//override getArea
public double getArea()
{
  return 2 * super.getArea() + getCircumference() getHeight();
}
//override getVolume
public double getVolume()
{
  return super.getArea() * getHeight();
}
//override getName
public String getName()
{
  return �Cylinder�;
}
}
Hosted by www.Geocities.ws

1