Vivian Maya
Vivian Maya

Reputation: 518

java wildcard type safety warning

Well, I have an interface which is;

public interface abc {
    public<T extends JPanel> T initalize();
}

And I'm implementing it. Here is thing, when I defining function like:

public class Startup_thePanel extends JPanel implements abc {
   public Startup_thePanel initalize() {

            return this;
    }
}

I'm getting warning on function initalize which is 'Type safety: The expression of type ... needs unchecked conversion to conform to ...'.

I can get rid of this with using suppresswarning but I do not want to use it. What am I missing ?

Thanks in advance...

Upvotes: 5

Views: 172

Answers (2)

Muhammad Suleman
Muhammad Suleman

Reputation: 2922

try this

public interface abc<T extends JPanel> {
    public T initalize();
}


public class Startup_thePanel extends JPanel implements abc<Startup_thePanel> {

    private static final long serialVersionUID = 1L;

    @Override
    public Startup_thePanel initalize() {

            return this;
    }
}

Upvotes: 4

Eugen Halca
Eugen Halca

Reputation: 1785

public interface abc<T extends JPanel> {
    public T initalize();
}

public class Startup_thePanel extends JPanel implements abc<Startup_thePanel> {
   public Startup_thePanel initalize() {

            return this;
    }
}

this would make the compiler to know which type of interface your are implementing.

Upvotes: 6

Related Questions