• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Constructors Problem

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

What will be the output? Answer: 1 2 41 42 1 2
why for yo, parents constructor is called. Shouldnt the child constructor have been called.
 
garima jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source is http://nikojava.wordpress.com/2008/10/02/scjp-mock-exam-for-overriding-overloading/
 
garima jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source: http://nikojava.wordpress.com/2008/10/02/scjp-mock-exam-for-overriding-overloading/
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at my first explanation in this thread, it answers your question:

Covariant problem
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi garima jain,

The instance variable value is determined gy the refernce which is used to invoke it. It is not depending on the object used.

In case of method calling, the case is just reversed. Then, the decision, which method is to be called(From Parent or Child) is taken depending on the object used- not on the reference.
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arron Ferguson:
Look at my first explanation in this thread, it answers your question:

Covariant problem



Thanks for you metion my post here.

I guess this is another compile time determination problem.

As you have see, the instance variables and static variables assignment have been decide by comiple time. In your case Parent, even you use new Child(), the reference type is a Parent type, so when you call yo.a and yo.b yo return's Parent type (decided in compile time). But if you have a method things are different, if Child have a method which override Parent method, then the same case yo.anoverride method will have Child type and call child method ( it decide at run time).

Hope you now have a clear idea on this.
 
garima jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks everyone...this is clear to me but now i'm confused in a similar problem..


Here the output is "granparent". Please explain. I have referred to web page post also.
 
Sheriff
Posts: 9692
42
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
Garima in the above code the object created is of type Parent. But parent doesn't have act() method. So act() of Grandparent will be called. Now you must be thinking that it must have displayed "Parent". But no, this will not happen. Whenever a method uses a field, it will use the field of the class in which it is or it's super class.

Eg



So basically you can say that fields are bound statically i.e. they don't care which object was used to call them. even if you would have written this display method in class Base



Then also the output would have been the same...
 
garima jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Ankit..I got it..this is really a good explaination
 
Look! I laid an egg! Why does it smell like that? Tiny ad, does this smell weird to you?
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic