JavaRanch Reference Thread -> http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=33&t=025141&p=1
Question by Jose Campana:
Analyze this case and please tell me Why the method with the comment //This method is never reached.
class Base{ int value = 0; Base(){ addValue(); } void addValue(){ //This method value += 10; } int getValue(){ return value; } } class Derived extends Base{ Derived(){ addValue(); } void addValue(){ value += 20; } } public class Test3 { public static void main(String[] args){ Base b = new Derived(); System.out.println(b.getValue()); } }
The preceding code prints out 40. And of course, that means that the method addValue() in class Base() never executes.
When inside Base() constructor, it calls the addValue() method, and it uses the one defined in class Derived. but the Question is:
WHY ?