BAEL-4156 Does a method's signature in Java include its return type? (#10049)

* BAEL-4156 Does a method's signature in Java include its return type?

* BAEL-4156 Add varargs

* Update OverloadingErrors.java

Co-authored-by: Cristian Stancalau <cstancalau@consultant.ffn.com>
This commit is contained in:
Cristian Stancalau 2020-09-23 19:23:50 +03:00 committed by GitHub
parent e3d721b72c
commit 20a3070093
2 changed files with 118 additions and 0 deletions

View File

@ -0,0 +1,72 @@
package com.baeldung.signature;
import java.io.Serializable;
public class OverloadingErrors<T extends Serializable> {
public void print() {
System.out.println("Signature is: print()");
}
/*
// Uncommenting this method will lead to a compilation error: java: method print() is already defined in class
// The method signature is independent from return type
public int print() {
System.out.println("Signature is: print()");
return 0;
}
*/
/*
// Uncommenting this method will lead to a compilation error: java: method print() is already defined in class
// The method signature is independent from modifiers
private final void print() {
System.out.println("Signature is: print()");
}
*/
/*
// Uncommenting this method will lead to a compilation error: java: method print() is already defined in class
// The method signature is independent from thrown exception declaration
public void print() throws IllegalStateException {
System.out.println("Signature is: print()");
throw new IllegalStateException();
}
*/
public void print(int parameter) {
System.out.println("Signature is: print(int)");
}
/*
// Uncommenting this method will lead to a compilation error: java: method print(int) is already defined in class
// The method signature is independent from parameter names
public void print(int anotherParameter) {
System.out.println("Signature is: print(int)");
}
*/
public void printElement(T t) {
System.out.println("Signature is: printElement(T)");
}
/*
// Uncommenting this method will lead to a compilation error: java: name clash: printElement(java.io.Serializable) and printElement(T) have the same erasure
// Even though the signatures appear different, the compiler cannot statically bind the correct method after type erasure
public void printElement(Serializable o) {
System.out.println("Signature is: printElement(Serializable)");
}
*/
public void print(Object... parameter) {
System.out.println("Signature is: print(Object...)");
}
/*
// Uncommenting this method will lead to a compilation error: java cannot declare both sum(Object...) and sum(Object[])
// Even though the signatures appear different, after compilation they both resolve to sum(Object[])
public void print(Object[] parameter) {
System.out.println("Signature is: print(Object[])");
}
*/
}

View File

@ -0,0 +1,46 @@
package com.baeldung.signature;
public class StaticBinding {
public Number sum(Integer term1, Integer term2) {
System.out.println("Adding integers");
return term1 + term2;
}
public Number sum(Number term1, Number term2) {
System.out.println("Adding numbers");
return term1.doubleValue() + term2.doubleValue();
}
public Number sum(Object term1, Object term2) {
System.out.println("Adding objects");
return term1.hashCode() + term2.hashCode();
}
public Number sum(Object term1, Object... term2) {
System.out.println("Adding variable arguments: " + term2.length);
int result = term1.hashCode();
for (Object o : term2) {
result += o.hashCode();
}
return result;
}
public static void main(String[] args) {
StaticBinding obj = new StaticBinding();
obj.sum(2, 3); // "Adding integers" due to auto-boxing from int to Integer
obj.sum(Integer.valueOf(2), Integer.valueOf(3)); // "Adding integers" due to exact parameter types
obj.sum(2, 0x1); // "Adding integers" due to type promotion from byte to int
obj.sum((Number) 2, (Number) 3); // "Adding numbers" due to explicit cast to Number
obj.sum(2.0d, 3.0d); // "Adding numbers" due to auto-boxing from double to Double
obj.sum(Float.valueOf(2), Float.valueOf(3)); // "Adding numbers" due to polimorphism
obj.sum((Object) 2, (Object) 3); // "Adding objects" due to explicit cast to Object
obj.sum(2, "John"); // "Adding objects" due to polimorphism
obj.sum(new Object(), new Object(), new Object()); // "Adding variable arguments 2"
obj.sum(new Object(), new Object[]{new Object()}); // "Adding variable arguments 1"
}
}