I was trying for days to figure out if is possible, I failed but maybe it is be possible(I think it should be possible).
Let's say we have a few UI components similar with Swing hierarchies + we will use fluent interfaces Fluent Interfaces:
public abstract class Component {
...
public abstract Component setName(String name);
public abstract String getName();
...
}
public abstract class Panel extends Component {
....
}
public abstract class TitledPanel extends Panel {
....
public abstract TitledPanel setTitle(String title);
public abstract String getTitle();
}
Is it possible, using generic to be able to write something like that?
new TitledPanel().setName("panel").setTitle("Title);
setName should return a TitledPanel instead of Component to be able to link those calls.
This is just a simple example, but the idea is once I have an object of type T any call to any fluent method of a superclass to return type T.
Edit 1: I forgot to exclude the part with overriding the methods and returning a covariant types :) I want just plain generics, if it is possible indeed.