File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Method Overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method Overloading" Watch "Method Overloading" New topic

Method Overloading

Krishna Tota
Ranch Hand

Joined: Mar 22, 2008
Posts: 40
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.


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
Rob Spoor

Joined: Oct 27, 2005
Posts: 20271

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).

How To Ask Questions How To Answer Questions
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
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.
I agree. Here's the link:
subject: Method Overloading
jQuery in Action, 3rd edition