Generics - in COBOL and Java

Table 1. Consuming
COBOL Java
*> Consuming a generic method via type inference

class-id Generics public.
01 point type IPoint[binary-double].
01 constrainedPoint type ConstrainedPoint[type PointImpl].
01 justAPoint type Point[string, string].

method-id init.
    *> initialize generic variables
    set point = new PointImpl()
    set constrainedPoint = new ConstrainedPoint[type PointImpl]()
    set justAPoint = new Point[string, string]()
    *> use generic variables
    invoke point::makePoint(1, 2, 3)
    invoke constrainedPoint::makeConstrainedPoint(point)
    set justAPoint::varT to "varT's value"
    set justAPoint::varN to "varN's value"
end method.
end class.
// Consuming a generic method 
public class generics {
    IPoint<Long> point;
    ConstrainedPoint<PointImpl> cPoint;
    Point<String, String> justAPoint;
    
    public void init() {
        // initialize generic variables
        point = new PointImpl();
        cPoint = new ConstrainedPoint<PointImpl>();
        justAPoint = new Point<String, String>();
        // use generic variables
        point.makePoint(1l, 2l, 3l);
        cPoint.makeConstrainedPoint(point);
        justAPoint.varT = "varT's value";
        justAPoint.varN = "varN's value";
    }
}

Portions of these examples were produced by Dr. Frank McCown, Harding University Computer Science Dept, and are licensed under a Creative Commons License.

Table 2. Defining
COBOL Java
*> Generic interface
interface-id IPoint using T
method-id makePoint using T (x as T, y as T, z as T).
end method.
end interface.

*> Generic class
class-id Point using T, N.
01 varT T public.
01 varN N public.
end class.

*> Generic class with constraints
class-id ConstrainedPoint using T.
constraints.
constrain T implements type IPoint[binary-double].
method-id makeConstrainedPoint using N (x as N).
constraints.
    constrain N implements type IPoint[binary-double].

end method.
end class.

*> Class with generic interface
class-id PointImpl
    implements type IPoint[binary-double].
method-id makePoint using T (x as T, y as T, z as T).

end method.
end class.
// Generic interface
interface IPoint<T> {
    void makePoint(T x, T y, T z);
}

// Generic class
class Point<T, N> {
    public T varT;
    public N varN;
}

// Generic class with constraints
class ConstrainedPoint<T extends IPoint<Long>> {
    <N extends IPoint<Long>> void makeConstrainedPoint(N x) {

    }
}

// Class with generic interface
class PointImpl implements IPoint<Long> {

    @Override
    public void makePoint(Long x, Long y, Long z) {

    }
}

Portions of these examples were produced by Dr. Frank McCown, Harding University Computer Science Dept, and are licensed under a Creative Commons License.