Jackie
Jackie

Reputation: 23539

Using generic type declarations in Abstract Java class

I have the following

public abstract class BaseImpl<T>{
  public T getObject();
} 

Now I want to extend the class like this

public class PersonImpl{
  //Works
  public Object getObject(){}
  //But I would like it to be like this
  public Person getObject(){}
}

I am not very good with generics but is this possible? Is there a better pattern here?

Upvotes: 0

Views: 34

Answers (1)

rgettman
rgettman

Reputation: 178263

Extend the generic version of BaseImpl, supplying the type you want:

public class PersonImpl extends BaseImpl<Person> {

Upvotes: 3

Related Questions