This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Overriding  RSS feed

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys!

Can someone point out where I am going wrong...



I would expect the code to print a = 10 b = 16. The compiler says,
b = 0 b = 16


Why does the variable b get printed when the constructor in the parent class is invoked the first time around?


Cheers
 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Just to give a complete answer, in case someone
overlooks some details. When you create a new
WhatsGoingOnHere instance in the main method,
the constructor of that class is called, and in its
turn it first invokes the superclass constructor:
this invocation was inserted by the compiler.

The superclass constructor ThisisOK( ) on it's
turn invokes the method call(). The method
call() is overridden in the subclass and it
will be the method that will be performed at runtime
since it's invoked with an instance of the subclass.

So, call() will print b=...
But, what happens is that, when the superclass
constructor is called, the member variable int b is
not initialized to 16 yet. Therefore the program
prints 0 where you expected 16.

Cheers,

Gian Franco

[ July 03, 2004: Message edited by: Gian Franco Casula ]

[Edited typo 6 -> 16]
[ July 03, 2004: Message edited by: Barry Gaunt ]
 
Anand Jayaraman
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gian!


Thanks for the help....



Cheers
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!