Home > Software design >  How to make the code compile and preserve functionality?
How to make the code compile and preserve functionality?

Time:03-31

i am new into java.. How do i fix this, so it will compile and preserve the functionality without changing dingo method. So this code should check if Pingo is a valid class.

class Pingo<T> {
    public void dingo(T t ) {
        System.out.println(t.bingoString( )) ;
    }
}

CodePudding user response:

The T is a generic type. In your case it's the Object class which doesn't have a method called bingoString. You need to create a class which has this method for the template. E.g.:

// still your class
class Pingo<T extends Blub> {
    public void dingo(T t) {
        System.out.println(t.bingoString());
    }
}
// a class which provides the signature
class Blub {
    public String bingoString() {
        return "test";
    }
}
  •  Tags:  
  • java
  • Related