Reputation: 482
public void delete10(){
T s = null;
try {
s=x.pop();
while(s.avg()<10)
s=x.pop();
}
I'm getting The method avg() is undefined for the type T
.Now I understand avg()
isn't implemented on type T
but it's implemented on the types that I will use instead of T
for this generic. How can I make it right?
public E pop() throws EmptyStackException {
if (empty()) {
throw new EmptyStackException();
}
E result = top.item;
top = top.next;
return result;
}
Upvotes: 0
Views: 82
Reputation: 692121
Assuming the avg method is declared in an interface Averageable, your class should be declared as
public class MyClass<T extends Averageable>
Upvotes: 3
Reputation: 79875
Have an interface with the avg
method listed. Then when you declare T
, write T extends yourInterface
.
Upvotes: 5