0

I can't call string methods on element and argument. The UML diagram tells me that StartsWith class has a Generic TypeT>String. I read in an other post you need to implement it in this way <T extends String>. I would like to substitute Object for T in the method here public boolean predicate(Object element, Object argument) but the compiler throws at me thats not possible.

interface:

public interface Intaf<T> {

    public boolean pres(T element, T argument);
}

class:

public class StartsWith <T extends String> implements Intaf {

    @Override
    public boolean pres(Object element, Object argument) {

        String firstLetterElement = element.substring(0,1);

        String firstLetterArgument = argument.substring(0,1);

        return firstLetterElement.equals(firstLetterArgument);
    }
}

3 Answers 3

1

You want to implement Intaf<T>. Just saying Intaf implies Intaf<Object>.

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

Comments

0

Change to

public class StartsWith <T extends String> implements Intaf<T> {

    @Override
    public boolean pres(T element, T argument) {

        String firstLetterElement = element.substring(0,1);

        String firstLetterArgument = argument.substring(0,1);

        return firstLetterElement.equals(firstLetterArgument);
    }
}

1 Comment

String is final. So T can only have the type of String.
0

You need to change as Intaf<T> and change the arguments as type T

@Override
public boolean pres(T element, T argument) {
 ....
}

Comments

Your Answer

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