7

Is it possible to dynamically identify T as a return type depending on subclass Type? I want something like the following:

public class Parent { public <T extends Parent> T foo() { return (T)this; } } public class Child extends Parent { public void childMethod() { System.out.println("childMethod called"); } } 

And then to call:

Child child = new Child(); child.foo().childMethod(); 

Without defining the type like so:

Child child = new Child(); child.foo().<Child>childMethod(); // compiles fine 

Thanks in advance!

4
  • Child extends Parent, right? Commented Jan 5, 2014 at 12:36
  • should Child extend Parent? Commented Jan 5, 2014 at 12:36
  • @chaplean what is the purpose to do like this: child.foo().childMethod()? The same result will be child.childMethod(). Commented Jan 5, 2014 at 12:56
  • Just to be able to write the inline statement. Commented Jan 5, 2014 at 13:04

2 Answers 2

4

You want this:

public class Parent<T extends Parent<T>> { public T foo() { return (T)this; } } public class Child extends Parent<Child> { public void childMethod() { System.out.println("childMethod called"); } } Child child = new Child(); child.foo().childMethod(); // compiles 
Sign up to request clarification or add additional context in comments.

9 Comments

simpler: public class Parent<T extends Parent> { }, class Child extends Parent { } and main : Parent<Child> instance = new Parent<>();
@solvator No. Your code results in a raw type Parent in the bound, which is very undesirable
ahh, yeach, did not notice..it would work if there wouldn't be inheritance
@Bohemian why did you use <T extends Parent<T>> instead of <T extends Parent>?
@solvator I've already answered that in my comment above, but here goes again... If you give Parent a generic parameter, every use of it should have that parameter, otherwise you get a raw type (one that hasn't been given a parameter. Although it looks like infinite recursion, Parent<T extends Parent<T>> is how you specify a generic bound to a subclass while avoiding using a raw type. When raw typing are used, all generic info is stripped from the class (almost, but not exactly, like it's Parent<Object>)
|
0

It is impossible in the Java type system for Parent to refer to the exact class of this. However, it can have a type parameter (say T) that subclasses can specify, as either themselves, or some other type (whatever they want), and use an abstract method to delegate the task of obtaining an instance of a that type T to the subclass.

public abstract class Parent<T> { // the implementer is responsible for how to get an instance of T public abstract T getT(); // in this case, foo() is kind of redundant public T foo() { return getT(); } } public class Child extends Parent<Child> { public Child getT() { return this; } public void childMethod() { System.out.println("childMethod called"); } } Child child = new Child(); child.foo().childMethod(); // compiles 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.