1-Code does not compile
2-Code throws Exception at runtime
why the output is float 4 and if so why when the constructor of B(float j) when it invokes its super costructor version of class A
don't cause a compile error .... as in class A we should have A(float j)....
this example from whzlabs ...
SCJP Pass 85%....
Thousands of candles can be lighted from a single candle, and the life of the candle will not be shortened
sub constructor that has no argument will have a super costructor that has no argument and then
sub constructor that has a string argument will have a super constructor that has a string argument
Who told you this?? Is this applicable to Test class which is a sub-class of B (and B's constructor takes float or short)?? There is no such rule that super class' constructor must take same arguments as sub-class constructor. If there was such a rule, what will happen to classes which don't have a subclass, would we change their constructors each time we created a subclass??
Joined: Apr 26, 2009
ok what about the issue of upcasting and downcasting here in that example??
The key to understanding this example is the line3 in Ankit's answer. When a subclass constructor executes if there is no explicit invocation to super() in some way in the first line then the compiler would automatically insert super(). If you don't want this default behaviour then you will have to explicitly invoke super with arguments super(args). It also means that if there is no no-args constructor in the parent class and there is a constructor with args then the subclass will not compile.