• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Confused about OOP fundamentals!!

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 74384
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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."
 
Sheriff
Posts: 22510
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic