By “undefined object as a parameter”, I assume you mean that you’re looking to write a function that doesn’t specify the type of the object in the function declaration, allowing you to only have one function.
This can be done with generics.
Instead of:
static void func(String str) { System.out.println("The string is: "+str); } static void func(Integer integer) { System.out.println("The integer is: "+integer); }
you can have:
static <T> void func(T value) { if (value instanceof Integer) System.out.println("The integer is: "+value); else if (value instanceof String) System.out.println("The string is: "+value); else System.out.println("Type not supported!! - "+value.getClass()); }
Test:
func("abc"); // The string is: abc func(3); // The integer is: 3 func(3.0); // Type not supported!! - class java.lang.Double
See Java generics for more information.