Brainteaser: Overridable methods

Consider the following case of inheritance:

public class  Parent {
   public Parent()  {
	getValue();
   }
   public void getValue()  {

   }
}

public class  Child extends Parent {
   private final Integer integer;
   public Child()  {
	integer = new Integer(888);
   }

   @Override
   public void getValue()  {
	System.out.println(integer);
   }
}

Question: What would the following program print, why?

public class  Test {
   public static void main(String[] args)  {
	Child child = new Child();
	child.getValue();
   }
}

Lets assume that getValue() implementation in Child class was changed to:

@Override
public void getValue()  {
     System.out.println(integer.toString());
}

Question: What would the output of the Test class be now, why?

More from Alexander Zagniotov:

  1. Brainteaser: Broken Comparator
    Question: The following program returns result “1″, which indicates that first Integer value is greater than the second, why? Please...
  2. Brainteaser: Hidden Iterators
    … While locking can prevent iterators from throwing ConcurrentMofdificationException, You have to remember to use locking everywhere a shared collection...
  3. Brainteaser: ArrayList VS TreeSet
    When I came across the following example I did not expect the results that the program has printed hehe… Question:...
  4. Brainteaser: Broken Case of Inheritance
    Consider the following case of inheritance: Created instance: Question: What value would s.getCounter() method return at this point and why?...
  5. Java Generics and Reflection
    Hi, the other day I had a situation, where in my code at run time I had to determine the...