• 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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!