Abstract class implement a generic Interface Type

miguel

This is a bit convoluted if you can follow along. I have this:

public interface Interface1<T> {
  void method1(T t);
}

public interface Interface2 { }

public interface Interface3 extends Interface1<Interface2> { }

abstract public class BaseClass<P extends Interface1> {
  P p;

  void method2() {
    p.method1(this);
  }
}

public class Concrete extends BaseClass<Interface3> implements Interface2 {
}

Interface2 and Interface3 will change with different implementations of BaseClass. The problem is in BaseClass method2 because this is not of type Interface2 which is what it expects in this specific case. It compiles and runs but it warns about an unchecked call.

I tried implementing the interface in the base class instead of the concrete class like this

abstract public class BaseClass<P extends Interface1, V> implements V 

public class Concrete extends BaseClass<Interface3, Interface2> 

but Java doesn't like it. Any ideas how to do this?

Troncador

To solve

interface Interface2 { }

interface Interface1<T> {
  void method1(T t);
}

interface Interface3 extends Interface1<Interface2> { }

abstract  class BaseClass<P extends Interface1<Interface2>> 
     implements Interface2{
  P p;

  void method2() {
    p.method1(this);
  }
}

class Concrete extends BaseClass<Interface3> implements Interface2 { }

It compile without any warning, the only change I made is:

abstract class BaseClass<P extends Interface1<Interface2>> implements Interface2

If you don't want to BaseClass implements directly Interface2 you can create a "marker":

interface InterfaceFOO {}   // <------------ Marker without methods

interface Interface2 extends InterfaceFOO{ } // <----- harmless extend

interface Interface1<T> {
   void method1(T t);
}

interface Interface3 extends Interface1<InterfaceFOO> { }

abstract  class BaseClass<P extends Interface1<InterfaceFOO>> 
     implements InterfaceFOO{
  P p;

  void method2() {
    p.method1(this);
  }
}

class Concrete extends BaseClass<Interface3> implements Interface2 { }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Implement Generic Abstract Entity class with dao interface and implemantation

From Dev

How to implement generic fields from abstract class?

From Dev

Casting in generic abstract class that implements interface

From Dev

Instantiation issue of generic Interface inherited by abstract class

From Dev

How to implement Interface to an abstract class which is constraint?

From Dev

How to implement Interface to an abstract class which is constraint?

From Dev

In Typescript, why isn't my generic abstract class finding the type parameter when I attempt to implement it?

From Dev

In Typescript, why isn't my generic abstract class finding the type parameter when I attempt to implement it?

From Dev

What does the class type constraint achieve if a generic type constraint must also implement an interface in c#

From Dev

Check if type is derived from abstract generic class

From Dev

Java generic abstract class return type mismatch

From Dev

instantiate object of abstract class with generic type

From Dev

Abstract class inheriting a generic type requiring a constructor

From Dev

Generic type constraint of new() and an abstract base class

From Dev

Abstract class inheriting a generic type requiring a constructor

From Dev

extending an abstract class with generic enum type

From Dev

"Class does not implement interface member" error on class implementing a generic interface

From Dev

Type is an interface or abstract class and cannot be instantiated

From Dev

I tried to implement abstract class and interface in a class and only interface members got called and abstract method didnt?

From Dev

How do I implement a generic class that has an constraint for a generic interface?

From Dev

How do you implement a Java Enumeration Abstract class and interface?

From Dev

How do you implement a Java Enumeration Abstract class and interface?

From Dev

Generic type whose type parameter is an abstract base class

From Dev

Defining generic property in abstract class as a type of the implementing class

From Dev

Constraint generic type for enum type to implement some interface

From Dev

Generic class with two type constraints, and a interface implementation

From Dev

Creating a class or interface for a complex generic type

From Dev

Implement interface with method returning class of type parameter

From Dev

Implement interface with method returning class of type parameter

Related Related

  1. 1

    Implement Generic Abstract Entity class with dao interface and implemantation

  2. 2

    How to implement generic fields from abstract class?

  3. 3

    Casting in generic abstract class that implements interface

  4. 4

    Instantiation issue of generic Interface inherited by abstract class

  5. 5

    How to implement Interface to an abstract class which is constraint?

  6. 6

    How to implement Interface to an abstract class which is constraint?

  7. 7

    In Typescript, why isn't my generic abstract class finding the type parameter when I attempt to implement it?

  8. 8

    In Typescript, why isn't my generic abstract class finding the type parameter when I attempt to implement it?

  9. 9

    What does the class type constraint achieve if a generic type constraint must also implement an interface in c#

  10. 10

    Check if type is derived from abstract generic class

  11. 11

    Java generic abstract class return type mismatch

  12. 12

    instantiate object of abstract class with generic type

  13. 13

    Abstract class inheriting a generic type requiring a constructor

  14. 14

    Generic type constraint of new() and an abstract base class

  15. 15

    Abstract class inheriting a generic type requiring a constructor

  16. 16

    extending an abstract class with generic enum type

  17. 17

    "Class does not implement interface member" error on class implementing a generic interface

  18. 18

    Type is an interface or abstract class and cannot be instantiated

  19. 19

    I tried to implement abstract class and interface in a class and only interface members got called and abstract method didnt?

  20. 20

    How do I implement a generic class that has an constraint for a generic interface?

  21. 21

    How do you implement a Java Enumeration Abstract class and interface?

  22. 22

    How do you implement a Java Enumeration Abstract class and interface?

  23. 23

    Generic type whose type parameter is an abstract base class

  24. 24

    Defining generic property in abstract class as a type of the implementing class

  25. 25

    Constraint generic type for enum type to implement some interface

  26. 26

    Generic class with two type constraints, and a interface implementation

  27. 27

    Creating a class or interface for a complex generic type

  28. 28

    Implement interface with method returning class of type parameter

  29. 29

    Implement interface with method returning class of type parameter

HotTag

Archive