• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confused about OOP fundamentals!!  RSS feed

 
Yash Sharma
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I must have forgotten all my OOP fundamental. I had this question at Javablackbelt.com and am not sure why the answer should be m from B, n from A. The answer says that method n is overloaded in subclass but I though overloading only works in the same class. Can I request you to please explain the behaviour?

 
Abhinav Trivedi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
brother it is not overloading ...it is overidding.....and in the first class method name should be the same if you are doing overloading (however argument differs). Read something on overloading and overriding then you will understand.
 
Yash Sharma
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhinav,

I guess you didn't read the question or try to dry-run the program

There are two methods m and n, out of which m is surely overridden in the subclass. My question are two fold:
1) Why does the main method when run, prints "m from B, n from A" instead of "m from B, n from B"?
2) Why does the Javablackbelt answer says that method "n" is overloaded?

Regards,

Yash

P.S. If moderator deem it correct they may move the question to Beginners forum.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yash Sharma wrote: . . . P.S. If moderator deem it correct they may move the question to Beginners forum.
I personally think this question is more difficult than "beginner's level."
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yash Sharma wrote:1) Why does the main method when run, prints "m from B, n from A" instead of "m from B, n from B"?

It prints "m from B" because method "m" is overridden. Method "n" is not overridden though, so when you call "a.n(b)" it uses the reference type and sees only one method "n" in class A. If you would call "b.n(b)" it would see the overloaded method and call that.

2) Why does the Javablackbelt answer says that method "n" is overloaded?

To override a method, the name and parameters must be exactly the same. Since the parameter type is different it is overloading, not overriding.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!