Quite often I have a hierarchy of classes where
I want a specific accessor method to be
present in all classes. Therefore I specify
it in the base class. But the information
that this function returns only depends on the
actual class, so it should static, shouldn't it?
We use the term "method", instead of "function".
In addition to all of the other good answers,
I'll reply that you may be looking for an
abstract method in the abstract base class. The
concrete subclass provides that actual "smarts" for
the method and it can vary by subclass.
A simple-minded example:
==========================
public abstract class Polygon
{
public abstract int area();
public abstract int perimeter();
public Polygon()
{
super();
}
}
public class Rectangle
extends Polygon
{
protected final int side1;
protected final int side2;
public int area()
{
return side1 * side2;
}
public int perimeter()
{
return 2 * (side1 + side2);
}
public Rectangle(final int theSide1, final int theSide2)
{
super();
side1 = theSide1;
side2 = theSide2;
}
}
public class Square
extends Rectangle
{
public int perimeter()
{
return 4 * side1;
}
public Square(final int theSide)
{
super(theSide,theSide);
}
}
==========================
The area() and perimeter() instance methods
are abstract in the base class. The concrete
subclasses provide their implementations that
are appropriate for their subtype. Anywhere
that the client code uses a Polygon, a Rectangle
or Square could be the actual type. Anywhere
that a Rectangle is used, it could be a Rectangle
or a special case of a rectangle called a Square.
I suggest using a factory method to instantiate
the concrete subtype and return the abstract
base type. The factory method could use parameters
to determine which actual subtype to create, thus
insulating the client code from knowing the actual
subtype.
--
----------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS for FREE!