• 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

question from Sahir's Mock Exam

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could anyone please explain why the following code produces 40 instead of 30?

Thank you,
Tina
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tina
You are creating a Derived object so the addValue method that is called in both the Base constructor and the Derived constructor is the Derived class' addValue method.
Hope that helps
 
Tina Ang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it! Thanks Dave!
Tina
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tina Ang:
Could anyone please explain why the following code produces 40 instead of 30?

Thank you,
Tina


Could you help me with understanding this as I don't see why the answer isn't 20?
I thought:
Base b = new Derived();
would call the constructor of the Derived class only so would increment 'value' by 20 once.
and then:
System.out.println(b.getValue());
would just return '20' as getValue() doesn't increment anything
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
martin
The first thing that happens when a costructor is called is if there is no call to a superclass constructor and no call to a different constructor of the same class (using this( )) then an implicit call is made to the default superclass constructor.
For the exact details check out theJLS section 12.5.
So basically the steps are:
1 -- executing the line 'Base b = new Derived();' causes control to go to the constructor for the Derived class.
2 -- Becasue there is no 'this' call and no call to 'super' the Base class default constructor is called.
3 -- in the Base constructor the method addValue is called, because the underlying object is a Derived object it calls the addValue method of the Derived class.
4 -- The addValue method of the Derived class adds 20 to the value of the value variable. Then it returns.
5 -- After the addValue method returns the Base constructor returns to the Derived constructor and start executing at the 1st line.
6 -- The 1st line of the Derived constructor is another call to addValue - again it is the Derived classes addValue method that is called.
7 -- Another 20 is added to the value variable.
8 -- The Derived constructor is done and a Derived object is returned and stored in the variable 'b'.
Hope that helps you out.
 
martin rolph
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks dave, that explaination really helps. I'm learning from the "Core Java 2 - Fundamentals Vol 1" book and that doesn't seem to mention that constructors by default automatically call their superclasse's constructor.
what was confusing me is the effect of adding super();
e.g My understanding now is that if I changed the Derived constructor to:

it would do exactly the same thing and follow the same events?
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Martin
That is exactly right. The call to super that is put in by the compiler if there isn't already one is the same as super( ).
Now play with it and add another superclass method that initializes the value variable to some value that is passed into it and then see what happens.
new Base constructor
public Base(int i){
value = i;
}
then call this one from the Derived class constructor. The best way to learn is to play with things and change them and guess what'll happen. Then, if it doesn't happen, try to figure out why.
 
martin rolph
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks again.
I've been trying out a few things with constructors which has proven very educational.
Just for the sake of it I was trying to think of a way to prevent the the super class's default constructor from being called (assuming there is one).
e.g.

when creating a Derived object by doing
Derived d = new Derived();
or Derived d = new Derived(new String("hello"));
is there anything I can put on line 1 to stop the Base() method from being called? e.g. anything special I can do evolving super() or this()?
I don't know why you would ever want to do this but I'm curious if its possible.
thanks.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Martin,
no it's not possible, short of throwing an exception and stopping the object construction process.
Think of it this way: the object you are constructing is layered like an onion, with the inner layers representing the superclasses, and the outermost layer is the actual base class you are instantiating. The object needs to initialize all the layers, from the inside out. That's why there's a call chain for constructors. When you call a constructor, the very first thing it wants to do is call its super() constructor, all the way up the chain until Object is actually reached.
You can defer the super() call by inserting a this(), to call another constructor in the same class, sort of like a lateral pass in football. But eventually, one of the constructors in your base class must call its superclass constructor.
[ April 18, 2002: Message edited by: Rob Ross ]
 
martin rolph
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks rob. I think I was just getting confused thinking that constructors worked the same way as method overriding, but now I see they don't!
 
Tell me how it all turns out. Here is a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic