6

i have a question to Java Overload Methods.

Suppose i have an overload methods foo:

public static String foo(String x) {
    return "foo-String: " + x;
}

public static String foo(Object x) {
    return "foo-Object: " + x;
}

How can I implement to functions like

public static String useString() {
    return(foo("useString"));   
}
public static String useObject() {
    return(foo("useObject"));   
}

of which one uses the overloaded string method, and one the overloaded object method?

The call of the foo-Method should use an String input. (that means i do not want to work with a cast like

return(foo((Object)"useObject")); 

Maybe you can help me with this problem

EDIT:

Above, just is an example for an exercise. I am trying to understand Overloads and Dispatch better and was looking for alternative solution for calling (and selecting) an overload method.

Aiko West
  • 791
  • 1
  • 10
  • 30
  • 3
    The question is, why you are doing this. You may have thought wrongly. And you should avoid Object there, as every class can be taken... Maybe it would help to explain your main goal. – Luftbaum Oct 27 '17 at 14:08
  • Dexter, what are you trying to achieve? you want to call inside a method one or the other overload methods? based on what? – developer_hatch Oct 27 '17 at 14:10
  • 2
    @DamianLattenero Two of the downvote reasons are "not useful" and "unclear". Doing something like this is not useful (or it has yet to be demonstrated). From your later comment, it's also unclear. You need more clarification to be able to answer. Those are both reasons for downvotes. – Sotirios Delimanolis Oct 27 '17 at 14:12
  • 2
    The answer is in the question: you need to use a cast. Of course, if you arbitrarily decide that you don't want to use what Java offers as a solution, you're screwed. That's like asking how to add integers, but refusing to use + and -. – JB Nizet Oct 27 '17 at 14:29
  • I am voting to reopen this question because 1) It follows all the guidelines required for posting a good question 2) It's a perfectly valid question asking for an alternate approach to do something 3) It may be a basic question for exploring the language features but it's definitely not unclear. The OP clearly explains the requirement. Hoping that others will see this and vote to reopen a good question. – Chetan Kinger Nov 12 '17 at 06:39

3 Answers3

3

If we keep aside the motive behind this for a second and try to answer the direct question, you can create an object reference instead of using an explicit cast.

public static String useObject() {
    Object obj = "useObject";
    return foo(obj);   
}

While the end result is the same, you avoid the need to have an explicit cast with this approach.

Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
1

I guess this is a bit of cheating, but strictly speaking this doesn't use a cast on the syntax level:

public static String useObject() {
    return(foo(Object.class.cast("useObject")));   
}
SpaceTrucker
  • 13,377
  • 6
  • 60
  • 99
1

If you want to use the method that receive an Object, you can upcast your string to object:

public class Main {
    public static String foo(String x) {
        return "foo-String: " + x;
    }

    public static String foo(Object x) {
        return "foo-Object: " + x;
    }

    public static String useString() {
        return(foo("useString"));
    }
    public static String useObject() {
        Object x = "useObject";
        return(foo(x));
    }

    public static void main(String[] args) {
        System.out.println(Main.useObject());
    }
}

The method public static String foo(Object x) will be called

developer_hatch
  • 15,898
  • 3
  • 42
  • 75