OO Programming with Java


Advanced Concepts

  1. A Function is an object's type similar to other objects:
public class Funs {
  public interface Fun { public int apply(int x); }
  public static void main(String[] args) {
    Fun abs = new Fun() { public int apply(int x) 
      { return (x>=0)?x:-x; }};
    System.out.println(abs.apply(-3));
  }
}

And with types' parameters:

  public interface Fun<X,Y> { public Y apply(X x); }
  public static void main(String[] args) {
    Fun<Integer,Integer> abs = new Fun<Integer,Integer>() 
      { public Integer apply(Integer x) { return (x>=0)?x:-x; }};
    System.out.println(abs.apply(-3));
  }

4 - 17