1

As we know if we want to overload an existing method, we should somehow change the number of parameters or the type of parameter. Here is my dilemma,I want to use overload with set type binding different subtypes

private boolean usedOverlap(Set<Variable> useVars, Set<Value> list) {
    // TODO Auto-generated method stub
    for(Variable use:useVars){
        if(list.contains(use.getValue()))
            return true;
    }
    return false;
}

private boolean usedOverlap(Set<Value>vaset_A,Set<Value>vaset_B){
    Set<Value>intersection = new HashSet<Value>(vaset_A);
    intersection.retainAll(vaset_B);
    if(intersection.isEmpty())
        return false;
    else
        return true;
}

There goes the problem how can I use overload in this case above. For the first parameter In the first method : Set second method: Set

4
  • 1
    What is the difference, exactly? The issue with overloading is, as far as I can see, there isn't one? Commented Oct 15, 2015 at 1:20
  • In the first method we use Set<Variable> and second we use Set<Value> Commented Oct 15, 2015 at 3:46
  • I don't think you can do that. As, in runtime, the type will be erased which leave those two functions to have same method argument (two Set) Commented Oct 15, 2015 at 4:20
  • I mean what is the difference in the end result of the two methods--do they do the same thing on the same two collections, or are they different? If same, eliminate one. If different, rename one. Commented Oct 15, 2015 at 10:02

1 Answer 1

3

You are misjudging method overloading: It cares type of the argument[Set] but not its generic subtypes[Set<Integer>]. Also the generics do play role only till compile time.

Update:

Java is a strictly type checked language. So the below overloading is possible.

 public boolean overloadable(String b) {
       //....
        return false;
    }

    public boolean overloadable(Object a){
      //...
            return true;
    }

    public static void main(String[] args) {
        Sample sample = new Sample();
        System.out.println(sample.overloadable(12));//call Object arguement method
        System.out.println(sample.overloadable("12"));
    }

output is :

true
false

Eventhough String is a subclass of object, since java strictly check the type, the compiler can determine correct method based on it.

Coming to your case consider the type of arguement(say arg) is Set not Set of <Type>.

More clearly arg instanceof Set >> true But
arg instanceof Set<Type> >> Compilation error

So compiler will consider both (Set and Set) as of same type Set.

For understanding, in case of collection the type determine the content of the Collection and not determining the type whether it is a List or Set

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.