Matt
Matt

Reputation: 482

Generics and methods

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

Answers (2)

JB Nizet
JB Nizet

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

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79875

Have an interface with the avg method listed. Then when you declare T, write T extends yourInterface.

Upvotes: 5

Related Questions