Does two generics method at the same call can cause a compile error when trying to use them?

Eitanos30 :

I'm read Deital's book :"Java How To Program", and in Generic Chapter the sentence is written:"If the compiler doesn’t find a method declaration that matches a method call exactly, but does find two or more methods that can satisfy the method call, a compilation error occurs". Can someone give me an example of the situation, because when i didn't success to get the above compile error. my code:

public class Animal
{
    public Animal ()
    {

    }

    public String bark()
    {
        return "Animal";
    }
}

public class Dog extends Animal  
{
    public String bark()
    {
        return "howhow";
    }
}

public class DogSon extends Dog
{
    public String bark()
    {
        return "I'm  Dog Son";
    }
}

public class Test
{
    public static void main(String[] args)
    {
        Test t = new Test();
        DogSon d = new DogSon();
        System.out.println(t.helpMethod(d));
    }

    public <T extends Dog> String helpMethod(T dog)
    {
        System.out.println("aaa");
        return dog.bark();

    }
    public <T extends Animal> String  helpMethod(T animal)
    {
        return animal.bark();
    }
}   

Since there isn't method that get exactly son object, and there are two generic methods that can be suit here. Isn't this the situation Deital talking about?

Z3d4s :

What you have to understand is called Erasure of Generic Methods

It means, the method's type parameter is converted to Object if it's unbound
or
it's first bound class when it's bound.

Bound means, -staying at your example- you have declared a relation of the type parameter T to another class, like you did:

// 1. method  
public <T extends Dog> String helpMethod(T dog)
// 2. method
public <T extends Animal> String  helpMethod(T animal)

T is bounded to Dog in the 1. method, and bounded to Animal in the 2. method.

So, when it comes to compiling, Java changes the T into Dog in the 1. method, and to Animal in the 2. method.
There is no ambiguity here, they are two different methods.

However, if you would declare a 3. method:

// 3. method
public <T> String helpMethod(T dog)

Then the T is unbound, you haven't declared a relation between T and another class, therefore when it comes to compiling, Java changes the T into Object.

Now, if you would try to declare a 4. method:

// 4. method
public  String helpMethod(Object dog)

There would be a compilation error, as the 3. method during type erasure would have the exact same method signature as your 4. method.
The compiler cannot decide which one you want to call, and throws an error.

With the above in mind, the short answer is:

In you code, you are using two different methods, there is no ambiguity, so no error occurs.

If you want to see that compilation error, you should declare another generic method which signature after compiling would be the same as an existing generic/non-generic method in your class.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=323132&siteId=1