Inheritance overflow…

Been fiddling with Java lately when I ran into the following cute quiz about inheritance.

Reading some articles about what’s Good/Bad in Java – I ran into the following code from an article called why Java sucks?:

class Boo{}
class Buzz extends Boo{}

class Play{

    static void foo(Boo b){
        System.out.println("in foo(Boo b)");

    static void foo(Buzz b){
        System.out.println("in foo(Buzz b)");

    public static void main(String...args){
        Boo b = new Buzz();
  • What would you think will be printed ?
  • Do you know why ?

Answer: variable b is declared as a reference to object of type Boo during compilation, and that’s why foo(Boo b) is chosen over the “more intuitive” option.

The reason that it works this way is that Java (as well as many other OO languages) is single dispatch. There are other languages like Common Lisp that supports “multiple dispatch”: the decision which version of a method will be called is based both on the class of the object as well as the run-time types of the arguments!

By the way, “multiple dispatch” behavior can be achieved in “single dispatch” Languages by using the Visitor pattern (described in “design patterns” book).

I started a list of my own about the things I like/don’t-like in Java and here’s the outcome:

Plus Minus
write once – run anywhere still slower than C++
Many good libraries (both java/javax as well as 3rd parties) no multiple inheritence
Garbage collection horrible character escaping when using regex
boxing can’t pass methods as a parameter
inner classes + anonymous classes handling primitives differently (why not treat them as objects?)
reduces boilerplate code every version (generics, try with resources, filters) still too verbose (open a file…)
If inner class wants to use a variable of the parent – it must be declared final
checked exceptions can be a pain with interfaces
Inheritance overflow…