4

Consider the following generic interface:

interface Petlover<T>{
    void train(T somePet);
}

I understand that it's possible to provide generic implementations to generic interfaces (e.g., class MalePetLover<T> implements Petlover<T>). But I am having trouble implementing the interface for a specific type only:

class Dogperson implements Petlover<T> {
    int age;

    void train(String someDog){...};
}

The compilation error is train(String) in Dogperson cannot implement train(T) in Petlover. What would be a right way to handle this?

2
  • implements PetLover<String>? You should clarify what you are trying to achieve here Commented May 14, 2017 at 13:05
  • The String is intended to be the name of a dog. I tried Petlover <String> to no avail (please see Eran's answer below) - the same error is output by the compiler. Commented May 14, 2017 at 13:13

2 Answers 2

6

Since you expect train to accept a String, your class should implement Petlover<String>:

class Dogperson implements Petlover<String> {
    int age;

    public void train(String someDog) {...};
}

or perhaps the train() method of Dogperson should accept a Dog argument, and then the class would implement Petlover<Dog>.

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

6 Comments

I seem to be getting the same error after changing T to String as you suggest: error: train(String) in Dogperson cannot implement train(T) in Petlover. Yes, it would be better to use a Dog object, but I just kept it as String for simplicity.
@flow2k It passed compilation for me. You just have to add public access modifier to the train() method.
Ah - okay. I missed that in your answer. Thanks.
Side note: You should follow the Java naming conventions.
@LewBloch Do you mean CamelCase?
|
3

The subclass that extends a generics class has to conform to the type that it specifies in its own declaration.

You declare : class Dogperson implements Petlover<T> {, so the train() method has to have this signature : void train(T someDog){...};

If you want to have this signature in the subclass:

void train(String someDog){...};

you have to declare a subclass that implements PetLover by specifying the String class as parameterized type :

class Dogperson implements Petlover<String> {
     public void train(String someDog){...};
}

You can specify any parameter as T derives from Object in the interface.
For example, if you want to have a Dog as type used in the subclass, you could write it :

class Dogperson implements Petlover<Dog> {
     public void train(Dog dog){...};
}

For example, if you want to have a Cat, you could write it :

class Catperson implements Petlover<Cat> {
     public void train(Cat cat){...};
}

2 Comments

Like Eran mentioned in the other answer, the train methods in the Dogperson and Catperson classes need to be public.
@Erwin Bolwidt True. Interface methods has to be public. We would have seen it before.

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.