File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

super constructor

 
Heba Mahmoud
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 ...

 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Heba Mahmoud
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok what about the issue of upcasting and downcasting here in that example??
 
Neha Daga
Ranch Hand
Posts: 504
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when it comes to parameter passing there is no implicit downcasting but upcast can is implicit.
 
Rajeev Trikha
Ranch Hand
Posts: 85
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic