2016-12-25 11 views
3

これはおそらく愚かな質問ですが、なぜ以下のコンパイルが失敗したのか理解できません。Java汎用クラス階層と汎用実装

私のクラス階層

Dao.java 

public interface Dao<E extends Entity, S extends SearchCriteria> { 
    <E> E create(E e) throws Exception; 
} 

このダオの一般的な実装はあり

DaoImpl.java 

public abstract class DaoImpl<E extends Entity, S extends SearchCriteria> implements Dao<E, S> { 
    @Override 
    public <E> E create(E e) throws Exception { 
     throw new UnsupportedOperationException("this operation is not supported"); 
    } 
} 

そして

ProcessDaoImpl.java 

public class ProcessDaoImpl extends DaoImpl<Process, WildcardSearchCriteria> { 
    @Override // this is where compilation is failing, I get the error that create doesn't override a superclass method 
    public Process create(Process entity) throws Exception { 
     return null; 
    } 
} 

説明エンティティクラス階層の専門実装があり

Process.java 

public class Process extends AuditEntity { 
    // some pojo fields 
} 

AuditEntity.java 

public abstract class AuditEntity extends IdentifiableEntity { 
    // some pojo fields 
} 

IdentifiableEntity.java 

public abstract class IdentifiableEntity extends Entity { 
    // some pojo fields 
} 

Entity.java 

public abstract class Entity implements Serializable { 
} 

答えて

5

あなたはそうしないと、クラスで宣言された型のEを指すものではありません。最初ではなくスコープに定義Eタイプに<E>なしインターフェースと抽象クラス、E create(E e)方法で宣言しなければならないので

置き換えます:

public interface Dao<E extends Entity, S extends SearchCriteria> { 
    E create(E e) throws Exception; 
} 
によって

public interface Dao<E extends Entity, S extends SearchCriteria> { 
    <E> E create(E e) throws Exception; 
} 

の方法の210

て、それを置き換える:

@Override 
public <E> E create(E e) throws Exception { 
    throw new UnsupportedOperationException("this operation is not supported"); 
} 

によって:

@Override 
public E create(E e) throws Exception { 
    throw new UnsupportedOperationException("this operation is not supported"); 
} 
+0

私の悪いに変更し、それを、それはかなり初心者でした。ありがとう – Ritesh

2

問題がある:

この E
<E> E create(E e) throws Exception; 

は、クラス宣言のものと同じEではありません。名前がEであるの新しい型パラメータが宣言されていますが、バインドされておらず、クラスの外側のEをシャドウしています。

E create(E e) throws Exception; 
関連する問題