• 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

Constructor call

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I executed this code


class Just {
int a = 10 ;

Just ( ) {
call ( ) ;
}

void call ( ) {
System . out . print( "a = " + a + " ") ;
}
}

class Q05 extends Just {
int b = 16 ;

Q05 ( ) {
call ( ) ;
}

void call ( ) {
System.out.print( " b = " + b + " " ) ;
}
public static void main ( String args [ ] ) {
new Q05 ( ) ;
}
}

I got b=0 b=16 but i was expecting a=10 b=16 because main method is creating object of subclass so first super class constructor will get called and in turn super class constructor is calling call() method. so it should call call() of just class.why it is calling call() of subclass method,

Please clarify my doubt.

Source:Voodoo mock exam Test3
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is because the object that is created is of the subclass type.
This is what is polymorphism.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gunjan,




Q05 overrides the call() method of its parent class Just.
When you create new object of subclass Q05, in its constructor super() is
automatically placed by the compiler.

OK now in the parent class method call() is called, and because the subclass
overrides the call() method, the subclass version of the call() method is called;

Real concern:
See the order of initialization in any book:
Until the parent class completes, member variables of the subclass get their
default value, (default means 0 to int, 0.0 to float, null to a reference variable and so on)

Second output is b=16, and that is explicit value because at this time,
subclass constructor call the call() method, and explicit value of the b is
printed (is has been initialized before the subclass constructor completes)

Please place the code in between tags.



Regards,
cmbhatt
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly,

if you debug it using breakpoints, the compiler will tell/show you that during the call of the method call() in the superclass constructor the this object is of type Q05.

Good luck.
 
Gunjan Kumar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all of you.

I got the point
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Folks

In above code

b=16 I am ok with that
but b=0 i cannot understand.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic