2

I was going through the method overloading in java and I was trying the output of the below program in eclipse , the program is ..

public class OverloadingTest {

    public static void main(String args[]){
       List abc = new ArrayList();
       List bcd = new LinkedList();

       ConfusingOverloading co = new ConfusingOverloading();
       co.hasDuplicates(abc); //should call to ArryList overloaded method
       co.hasDuplicates(bcd); //should call to LinkedList overloaded method
    }


}

class ConfusingOverloading{

    public boolean hasDuplicates (List collection){
        System.out.println("overloaded method with Type List ");
        return true;
    }

    public boolean hasDuplicates (ArrayList collection){
        System.out.println("overloaded method with Type ArrayList ");
        return true;
    }


    public boolean hasDuplicates (LinkedList collection){
        System.out.println("overloaded method with Type LinkedList ");
        return true;
    }

}

and the output is ..

Output
overloaded method with Type List
overloaded method with Type List

Now in the explanation it was told ..method overloading is resolved at compile time using static binding in Java, so please advise how can I achieve the same through method overriding.

2 Answers2

1

abc,bcd both are of type List even when you initialize it with a subclass.hence the result

A BaseClass like List helps you to write methods that can work with any of it's subclass(ArrayList or LinkedList). So,

public ArrayListLinkedListCanCallMe(List lst)
{
 //now imagine if this method was called with bcd as parameter
 //still lst would be of type List not LinkedList
 //and if lst were allowed to be of type LinkedList then how could List know any
 //of the methods of LinkedList.Therefore lst would always be of type List NOT LinkedList
}

You can instead try

co.hasDuplicates((ArrayList)abc);
co.hasDuplicates((LinkedList)bcd);

But (ArrayList)abc can throw cast exception if abc is of type LinkedList.You can use instanceof operator to check if abc is of type ArrayList and then cast it..

Anirudha
  • 32,393
  • 7
  • 68
  • 89
0

In this particular case, if hasDuplicates means what it says, I would have one method taking List as argument. It would just create a HashSet initialized with the List contents and compare its size to the List size.

If you really do need special case code for e.g. ArrayList you could use instanceof inside a List argument method.

However, when you are working with an interface type it is much, much better to find common approaches that will work for all implementations of the interface. If you cannot do that, you must allow for the possibility of being passed an object of a class that implements the interface but is not one of the classes for which you have special case code. If you need special case code for java.util.ArrayList, why don't you need special case code for instances of the private class that Arrays.asList uses for its result?

When the issue is different classes in your own code, you can often turn the problem around and put the method in what is currently the argument class, so that conventional overriding works.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75