• 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

Why the output is 2 here?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A
{
int a = f();
int f() {
return 1;
}
}

class B extends A
{
int b = a;
int f() {
return 2;
}
}

public class Test_ObjectInitialisation
{
public static void main(String args[])
{
B bobj = new B();
System.out.println(bobj.b);
}
}
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because at runtime the call in class A to f() has been overridden by B to return 2 instead of 1.

Prehaps it would be more clear if you said



Now would you expect it to print 1 or 2? The same principles are at work in your code. I would turn the question around, if f() has been overriden in B why would you expect the code to return 1?

Also you could format your code and this probably doesn't belong in advanced.
 
Sheriff
Posts: 67748
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please reserve this forum for advanced questions. Moved to Java in General(beginner).
[ October 10, 2006: Message edited by: Bear Bibeault ]
 
MukulJ Patil
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim, as per my information, following is the order in which the object get initialised:
1)Set fields to default initial values (0, false, null)
2)Call the constructor for the object (but don't execute the body the
constructor yet)
3)Invoke the superclass's constructor
4)initialisation
4)Initialize fields using initializers and initialization blocks in sub
class
5)Execute the body of the constructor

So i guessed when object of B is created, it will call it's constructor(default) which in tern calls super class constructor. Then does initialisation over there i.e. a=1; then it returns to class B, does initialisation i.e b=1;

Here i am not getting how overriding is coming into picture. Plese clarify.
Thanks.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

MukulJ Patil wrote:

Here i am not getting how overriding is coming into picture. Plese clarify.
Thanks.




Here initialization is done with a method call. And method calls are polymorph also when the method is invoked in a constructor.

e.g.



Will print
constructor A, i=1
constructor A, i=2
constructor A, i=2



The situation would be different, if the constructor would not call a method, but just said
i=1; (or 2)
because methods are used polymorphically (if not static) while variables are not.
The answer to your question has nothing to do with the order in which the object is initialized here.


Yours,
Bu.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code does not contain constructors (other than the default constructors inherited from the Object object) so while your statement about their order of execution may be correct, it doesn't have anything to do with why your output is 2.

The reason that your code outputs 2 is because your class B, which extends class A, overrides the f() method in class A with its own version of the same method. When you instantiate class B, after the initialization of the superclass variables and methods, then the subclass variables and methods are initialized and anything that has a duplicate label (e.g. the f() method) replaces the old value (whether it was a method or a property) with its own new method or property.
[ October 13, 2006: Message edited by: Joel Jorgensen ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joel,

Welcome to JavaRanch!

Originally posted by Joel Jorgensen:
Your code does not contain constructors (other than the default constructors inherited from the Object object) so while your statement about their order of execution may be correct, it doesn't have anything to do with why your output is 2.



Well.... actually, the code does contain a non-empty constructor in class A; and that constructor makes a polymorphic call to f(); and that's exactly why the output is 2. It's just that the constructor doesn't appear explicitly in the code. If a class has instance variable initializers, then the compiler inserts the code for them into each generated constructor, immediately after the superclass constructor call. If you were to compile the original class A and then disassemble it with javap, you'd see:



And that's exactly the constructor that Burkhard shows in his version of the code.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic