From 20a3070093b70bdc6b80c6cf1a8716537b366df8 Mon Sep 17 00:00:00 2001 From: Cristian Stancalau Date: Wed, 23 Sep 2020 19:23:50 +0300 Subject: [PATCH] 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 --- .../baeldung/signature/OverloadingErrors.java | 72 +++++++++++++++++++ .../com/baeldung/signature/StaticBinding.java | 46 ++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/signature/OverloadingErrors.java create mode 100644 core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/signature/StaticBinding.java diff --git a/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/signature/OverloadingErrors.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/signature/OverloadingErrors.java new file mode 100644 index 0000000000..4c9d11d925 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/signature/OverloadingErrors.java @@ -0,0 +1,72 @@ +package com.baeldung.signature; + +import java.io.Serializable; + +public class OverloadingErrors { + + 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[])"); + } + */ +} diff --git a/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/signature/StaticBinding.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/signature/StaticBinding.java new file mode 100644 index 0000000000..01016812f0 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/signature/StaticBinding.java @@ -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" + } +}