• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

parameter passing

 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Case A

Case B

My question is why in case B we are accessing the method in the superclass vs the overridden method in Subclass and is this always the case.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,
According to me in case B the method is not overridden but it is overloaded since the no. of parameters are different. also in the main() when u pass only one parameter the program call the appropriate method depending upon the no of parameters passed. Therefore it prints 4 if u pass 2 parameter the 2nd method will be invoked. This is my theory. Other guys please reply to verify my answer.
Mahesh
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case B, you overload the method of the superclass. So when running, it invoking the method getNumber(int a).
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Mahesh has it right. To override a method, the method signature of the overriding method must be the same as the method signature of the overridden method. To overload a method, you are just writing new methods with the same name as an existing one, but with different parameters. So, if a parent class has method(int a), it would be overridden by method(int b) in a subclass, while method(String a) in the given subclass would be overloading.
Making sense?
Good Luck,
-Dirk Schreckmann
 
Always look on the bright side of life. At least this ad is really tiny:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic