The purpose of overloading is to re-use the method signature.
Overloading methods cannot be validated by differing return types.
In the following case, imagine what will the output?
Overload Methods
Overloading is distinguished and validated only by input augment lists.
- The change in the number of parameters that are accepted.
- The change in the type of method parameters that are accepted.
- The change in the positions of the parameters that are accepted
public void method(float m1, int m2){}
public void method(int m2, float m1){}
The sequence of parameters can create different argument lists, so it is valid overloading.
Overloading methods cannot be validated by differing return types.
double calcAverage(int m1, int m2){}
int calcAverage(int m1, int m2){}
the above is invalid overloading and leading to a compiling error.
Overloading methods cannot be validated by differing access modifiers
Overloading method cannot be validated by differing non-access modifiers
When the class don't share an inheritance relationship, there isn't any confusion with the version of the method that will be called.
Class Base{}
Class A extends Base{}
Class B extends Base{}
A and B they both in a different branch of the inheritance tree, they are not a confusion;
However, A and Base are on the same inheritance branch; they may create confusion.
A is a Base; while Base is also Base; These two types may confusing the overloading.
Class Reception{
public void accept(Base var){} //1
public void accept(A var){}//2
}
If invoking accept(new A()); Which overloading method will be called?
here the A instance is created, so //2 should be invoked.
The overloaded methods are bound at the compiling time but not runtime.
In another way:
Base a = new A();
accept(a)
On the compiling time, a is considered as a Base class type because of the reference variable type, the //1 is therefore invoked.
Overloaded Constructors
- Overloaded Constructor must be defined using a different argument list.
- Overloaded Constructors cannot be validated by differing only the access modifiers.
Using this key to refer to other overloaded constructors in a constructor; it is a common mistake to use a Class name to refer to other overloaded constructors in a constructor.
Using non-access modifiers with constructors is illegal. The code won't compile.
Using non-access modifiers with constructors is illegal. The code won't compile.
In the following case, imagine what will the output?
/** * * @author YNZ */ class TestClass { void probe(int... x) { System.out.println("In ..."); } //1 void probe(Integer x) { System.out.println("In Integer"); } //2 void probe(long x) { System.out.println("In long"); } //3 void probe(Long x) { System.out.println("In LONG"); } //4 void probe(Object x) { System.out.println("In Object"); } //5 void probe(int x) { System.out.println("In int"); } //6 public static void main(String[] args) { Integer a = 4; new TestClass().probe(a); //5 Long b = 4L; new TestClass().probe(b); //6 byte c = 100; new TestClass().probe(c); char d = 'a'; new TestClass().probe(d); int e = 10; new TestClass().probe(e); String str ="aha"; new TestClass().probe(str); int[] array = new int[3]; new TestClass().probe(array); } }
No comments:
Post a Comment