Classes - in COBOL and Java

COBOL Java
 
                              				  
// 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.