This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method Overloading" Watch "Method Overloading" New topic
Author

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.

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

Joined: Oct 27, 2005
Posts: 19651
    
  18

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37953
    
  22
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.
 
 
subject: Method Overloading
 
Similar Threads
Overriding with Exception
Reg. Overriding : Valiveru's exam
Overloading and Overriding
question from mock exam
Overwriting and Overloading Question