• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Method Overloading

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I know that if a calss have two or more methods with the same name with different parametors we call that as Method Overloading.

Say,

public int aMethod(int a,int b){}

public int aMethod(int a,char b){}

this is called Method Overloading.


My query is What happens if i change modifier as static or other in place of public?
Will still it is called Method Overloading.

Can we perfom Method Overloading in two classes(Super and sub)?

Please replay in brief
Thanks,
Krishna
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's called method overloading if two methods available in the same class have the same name; the access modifiers do not matter.

That sentence implies that overloading will work with sub classes as well. Keep in mind that private methods are only available in the class itself; the following is not a case of overloading:

Class B will still only have one method called doSomething. It would be overloading if A.doSomething would be protected or public (or have default if A and B are in the same package).
 
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you change a method to static it still represents overloading, yes. You still have to make sure that the parameters are different types.

Yes, you can overload a method in a subclass, but it is probably not a good idea. Common mistake:This will compile and run, but many classes in the API will expect the equals method to be overridden not overloaded, so classes will look for a public boolean equals(Object o) method and may give incorrect or inapporpriate results.

And I see Rob Prime has beaten me by 6 minutes.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic