This week's book giveaway is in the iOS forum.
We're giving away four copies of Classic Computer Science Problems in Swift and have David Kopec on-line!
See this thread for details.
Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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!

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!