I'm trying to figure something out but googling is difficult since I'm not sure exactly what keywords are needed. Suppose I have a class like this:
class MyClass {
int i = 0;
public MyClass increment() {
i++;
return this;
}
}
and then I have a subclass:
class MySubClass extends MyClass { }
and then I want do the following:
MySubClass mySubClass2 = new MySubClass().increment();
The problem is that the object returned by increment
is recognized as an instance of MyClass
and not MySubClass
, so this won't work. Is there a way to declare increment
in MyClass
so that it always returns something of the subclass's type?