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

Inner class constructors and overriding  RSS feed

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The above code prints AABAB while it should print AAAB..Where am i mistaken??

Is it because The line ob.new test1() will call the sub class go() because of overriding?? Am i correct,,,,,,correct me if i m wrong
 
Ranch Hand
Posts: 310
1
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right there was a small syntax error in the code , here is the modified code


The test ob = new test() // will print A
ob.new test1() will invoke the parent constructor which is test() {go()}. Because of runtime polymorphism it calls the sub class go() which is

This adds next "AB" to output

Finally the sub class constructor will be called test1() {go() } which will again call sub class go() method which adds last "AB"

So the final output is "AABAB"


This makes a nice exam question I really hope I don't get these kind of questions!!!


Good luck!

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