• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method Overloading

 
Krishna Tota
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • 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
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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).
 
Campbell Ritchie
Sheriff
Pie
Posts: 50189
79
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic