• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

super constructor

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...

 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok what about the issue of upcasting and downcasting here in that example??
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when it comes to parameter passing there is no implicit downcasting but upcast can is implicit.
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic