Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an object with a String and method overloading

I have a String which can either be of Double or Integer type or some other type. I first need to create a Double or Integer object and then send it over to a overloaded method. Here's my code so far;

public void doStuff1(object obj, String dataType){

 if ("Double".equalsIgnoreCase(dataType)) {
        doStuff2(Double.valueOf(obj.toString()));
    } else if ("Integer".equalsIgnoreCase(dataType)) {
        doStuff2(Integer.valueOf(obj.toString()));
    }

 }

 public void doStuff2(double d1){
   //do some double related stuff here
 }

 public void doStuff2(int d1){
   //do some int related stuff here
 }

I'd like to do this without if/else, with something like this;

Class<?> theClass = Class.forName(dataType);

The problem is 'theClass' still can't be cast to either double or int. I would be gratefull for any ideas. Thanks.

Found a related thread; Overloading in Java and multiple dispatch

like image 817
Madz Avatar asked May 02 '12 10:05

Madz


People also ask

What is an example of an overloading method?

In Java, two or more methods may have the same name if they differ in parameters (different number of parameters, different types of parameters, or both). These methods are called overloaded methods and this feature is called method overloading. For example: void func() { ... }

How do you create a method overloading?

Method overloading can be achieved by the following: By changing the number of parameters in a method. By changing the order of parameters in a method. By using different data types for parameters.

Can we overload string?

Yes, by method overloading. You can have any number of main methods in a class by method overloading. But JVM calls main() method which receives string array as arguments only.

What is string overloading in Java?

Overloading allows different methods to have the same name, but different signatures where the signature can differ by the number of input parameters or type of input parameters or both. Overloading is related to compile-time (or static) polymorphism.


2 Answers

This is not just a problem of dealing with primitive types.

Which method to call is decided in compile time, that is, if you want to be able to call different methods depending on the type of the arguments, you'll need several calls (i.e. you need the if-construct).

In other words, it wouldn't work even if doStuff2 took Integer and Double as arguments (your code is basically as good as it gets).

(In fancy words, this is due to the fact that Java has single dispatch. To emulate multiple dispatch you either need to use conditional statements or a visitor pattern.)

like image 97
aioobe Avatar answered Oct 21 '22 09:10

aioobe


Since the method call is decided at compile time as the another answer told you, overloading won't work for you. I think that this problem can be solved with inheritance. So you write a base class with yourMethod() and override it in your derived classes.

like image 29
Adam Arold Avatar answered Oct 21 '22 09:10

Adam Arold