File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes super constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "super constructor" Watch "super constructor" New topic
Author

super constructor

Heba Mahmoud
Ranch Hand

Joined: Apr 26, 2009
Posts: 62


1-Code does not compile
2-Code throws Exception at runtime
3-Prints "float4
4-Prints "short4"
5-Prints "0"
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
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Since the Test class constructor calls super with value 4, so the value is automatically upcasted to float as it cannot be downcasted to short.
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).

Since in the constructor of class B we didn't explicitly call the super constructor, the compiler will insert the statement super(); as the first statement of the constructor. So it will look like

The implicit super call inserted by the compiler doesn't has any arguments...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Heba Mahmoud
Ranch Hand

Joined: Apr 26, 2009
Posts: 62
i thought that we should have super constructor that match with the sub constructor in arguments types ......

such as we can say as an example :

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


this is what i understand and sure may i have misunderstanding in that issue , so confrim me with true answer.

also for upcasting and downcasting ... i don't know accuratelly why it select upcasting than down casting !!
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

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??
Heba Mahmoud
Ranch Hand

Joined: Apr 26, 2009
Posts: 62
ok what about the issue of upcasting and downcasting here in that example??
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
when it comes to parameter passing there is no implicit downcasting but upcast can is implicit.


SCJP 1.6 96%
Rajeev Trikha
Ranch Hand

Joined: Jan 29, 2010
Posts: 85
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.


Rajeev Trikha (SCJP 6)
 
 
subject: super constructor