6

Assume the following existing classes:

class A { 
  public void foo() { ... };
  ...
}

class A1 extends A  { ... };
class A2 extends A  { ... };
...
class A1000 extends A  { ... };

now, we need to create a variant of each Axx class that overrides "foo" method. The basic idea was:

class B<T extends A> extends T {
  @Override public void foo () { ... };
}

But it seems is not posible to extend a class from one of their parametized types.

The objective is to skip the need of following new code:

class B1 extends A1 { @Override public void foo() { ... }; }; 
class B2 extends A2 { @Override public void foo() { ... }; }; 
....
class B1000 extends A1000 { @Override public void foo() { ... }; };

and allow statements like:

... 
B<A643> b643 = new B<A643>; 
b643.foo(); 
...

Any hint?

Thanks a lot.

3
  • 2
    Nowhere in your code is the type parameter T actually used for anything, hence you could just eliminate it from your code entirely. Also, if T really is a formal type parameter, then B extends T is not valid. Commented May 11, 2015 at 11:04
  • The objective is to skip the need of following new code: class B1 extends A1 { Override public void foo() { ... }; }; class B2 extends A2 { Override public void foo() { ... }; }; ... class B1000 extends A1000 { Override public void foo() { ... }; };. Clarified on the question. Commented May 11, 2015 at 12:11
  • I feel like what OP is trying to do is have a common way to extend any given type with this implementation of foo. For instance, A1, A2, ... A1000 are completely (or mostly) unrelated and each have their own methods, and B1, B2, ... B1000 are similarly unrelated, each having the exact same methods as their Annn counterparts as well as this common implementation of foo (which might or might not be an override or use A's protected members). Commented Aug 24, 2017 at 21:21

3 Answers 3

5

A isn't generic. I think you wanted something like,

class B<T> extends A {
  @Override public void foo () { ... };
}

That is a generic type B that extends A... T extends A would mean B takes a type that extends A (not B extends A).

Sign up to request clarification or add additional context in comments.

7 Comments

Hi, thanks for your colaboration. The objective is to skip the need to write something like "class B1 extends A1...", "class B2 extends A2..." with a generic "class B" like the one show in the question. The generic class is "B" and "A1"..."A1000" are the type parameters.
@Alu that isn't possible in java.
instrumentation and introspection?
Won't allow you to generate new classes based on a parametrized type.
Often, problems like this are best solved using the Collections framework API. You may think that you have classes Annn that need to extend Bnnn, but most likely your problem could be refactored to make better use of the things Java is good at: interfaces and classes.
|
1

You can mix inheritance with delegation. I'd consider it ugly, but it should work.

class UniversalB extends A{
 A a;
 UniversalB(A a) {
    this.a = a;
 }

 @Override public void foo() { ... };

 // @Override any other method from A you want/need
 // and delegate it to the passed member if necessary

}

UniversalB b = new UniversalB(new A123());
b.foo();
b.anyMethodInA();

2 Comments

Hi. Thanks for your colaboration. The problem in your proposal is when A123 calls "foo()" in some of its original methods. It will use the A123 implementation of foo, instead of the new one at "UniversalB".
Indeed it is. But it's impossible to create a class which has dynamic, compile-time unknown hierarchy. Templates are verified at compile time, but, as far as I remember, actually practically skipped at runtime. It's kind of a code-verifying syntactic sugar.
0

Finally, solved using a proxy class. Standard reflection.proxy was not applicable, but proxies from librarian CGLib does. In short, an interceptor with the same code than "B.foo" is used. See reflection.proxy not valid when override for details. Thanks to Tagir Valeev for his help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.