Is it possible to simultaneously and generally subclass both a limited general class and a common interface?

I am trying to create a new class by subclassing another common class (with binding) and implementing a common interface (without binding):

public class Foo1<T extends Bar> { ... } public interface Foo2<T> { ... } public class ProblemClass<T extends Bar, U> extends Foo1<T extends Bar> implements Foo2<U> { ... } 

This gives me a compilation of errors. I also tried:

 public class ProblemClass<T, U> extends Foo1<T extends Bar> implements Foo2<U> { ... } public class ProblemClass<T extends Bar, U> extends Foo1<T> implements Foo2<U> { ... } 

But neither one nor the other is working.

What is the correct syntax for defining my subclass in a way that allows me to maintain typing, allowing me to pass their types along with the superclass and interface? Is it possible?

Thanks!

+4
source share
4 answers

This declaration should work fine. What is your mistake? Which compiler are you using?

 class ProblemClass<T extends Bar, U> extends Foo1<T> implements Foo2<U> { ... } 

This is valid Java. If the IDEA compiler rejects it, IntelliJ has an error.

+2
source

From the code you posted, you only have the class Foo not Foo1. This is the reason? or just posting-edit error?

I think this should work

 public class ProblemClass<T extends Bar, U> extends Foo1<T> implements Foo2<U> { ... } 
+1
source

This does not give compilation errors with JDK 1.6.0_07

 public class Bar {} public class Foo1<T extends Bar> {} public interface Foo2<T> {} public class ProblemClass<T extends Bar, U> extends Foo1<T> implements Foo2<U> {} 
+1
source

This (from your question):

 public class ProblemClass<T extends Bar, U> extends Foo1<T extends Bar> implements Foo2<U> { ... } 

should be as follows:

 public class ProblemClass<T extends Bar, U> extends Foo1<T> implements Foo2<U> { ... } 

That is, you should not retell the boundaries when expanding Foo1.

With this correction, the code compiles in javac. If IntelliJ IDEA still refuses to compile it, maybe you have found an error or maybe the β€œBar” you are talking about in Foo.java, is it not the same β€œBar” in ProblemClass.java?

0
source

Source: https://habr.com/ru/post/1302820/


All Articles