| COBOL | Java |
|---|---|
*> Static class, no instances possible class-id Utils internal static. method-id Square (x as binary-long) returning return-value as binary-long static. set return-value to x * x end method. end class. *> Abstract class class-id Shape public abstract. 01 #Area binary-long protected property with no set. method-id ToString returning return-value as string override. set return-value to "Area = " & self::Area end method. end class. *> Sub class class-id Rectangle public inherits type Shape. 01 #Width binary-long property. 01 #Height binary-long property. property-id #Area binary-long override. getter. set property-value to #Width * #Height end property. end class. *> Sub class of a sub class class-id Square public inherits type Rectangle 01 #Size binary-long property. 01 #Width binary-long property override. 01 #Height binary-long property override. property-id #Area binary-long override. getter. set property-value to type Utils::Square(#Size) end property. method-id ToString returning return-value as string override. set return-value to "Square, " & super::ToString() end method. end class. |
// Closest possible form of a static class
final class Utils
{
private Utils() { }
public static int square(int x)
{
return x * x;
}
}
// Abstract class
public abstract class Shape
{
protected abstract int getArea();
public String toString()
{
return "Area = " + getArea();
}
}
// Sub class
public class Rectangle extends Shape
{
private int _width, _height;
public int getWidth() { return _width; }
public void setWidth(int value) { _width = value; }
public int getHeight() { return _height; }
public void setHeight(int value) { _height = value; }
protected int getArea() { return _width * _height; }
}
// Sub class of a sub class
public class Square extends Rectangle
{
private int _size;
public int getSize() { return _size; }
public void setSize(int value) { _size = value; }
public int getWidth() { return _size; }
public void setWidth(int value) { _size = value; }
public int getHeight() { return _size; }
public void setHeight(int value) { _size = value; }
protected int getArea() { return Utils.square(getSize()); }
public String toString()
{
return "Square, " + super.toString();
}
}
|
Portions of these examples were produced by Dr. Frank McCown, Harding University Computer Science Dept, and are licensed under a Creative Commons License.