aspose file tools
The moose likes Beginning Java and the fly likes overloading and overriding doubt! Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "overloading and overriding doubt!" Watch "overloading and overriding doubt!" New topic
Author

overloading and overriding doubt!

pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
Hi ,

Should overloading happen in the same class? Can overloading happen in two different classes?
like say you have

class A {
void fun() {
}
}
class B {
void fun(int x) {
S.O.P(x);
}
}
/*** now is the above overloading************/

second doubt :

say you have

package a;

public class Xyz {
void boo() {
}
}

package B;
import a.*;

class Abc {
void boo(int b) {
S.O.P(x);
}
}
/********* now is this overloading******/

third doubt:

Overriding happens only when we talk about inherited classes is it?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Originally posted by prasanna sheregar:
Overriding happens only when we talk about inherited classes is it?

Correct.
A method is being overridden if:
- the return type and parameters are exactly the same*
- the method exists in a parent class (doesn't have to be the direct parent)
- the method that is overridden is not private or static.

I've added the last line because you cannot override a private or static method - you reimplement them, hiding the method in the parent class.


* Actually, the return type can be narrower since Java 5. This is called "covariant return".


Now, overloading can only be done within the same class, so neither example is overloading.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
if the above programs i wrote is not overloading then what is it?

But it sattisfies all rules of overloading!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

All except one: the methods are in different classes!

If "void fun()" and "void fun(int x)" were both in class A, or both in class B, or class B extended class A or vice versa, then yes, this was overloading. The same goes to "void boo()" and "void boo(int b)".

For overloading, both methods need to be available (either directly or through inheritance) in the class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overloading and overriding doubt!
 
Similar Threads
doubt bank dare to answer 1
overloading minute doubt?
Inner Classes Mock question doubt 2
Doubt on package
Doubt on static