• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inner class constructors and overriding  RSS feed

 
ujjawal rohra
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
 
Rajeev Rnair
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!

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!