I have a superclass method which returns itself(for builder pattern). This class has several subclasses , so I want to return a reference to actual(subclass) type of the object. Something like this:
class SuperClass {
T someMethodThatDoesSameThingForAllSubclasses() {
// blablbal
return reference_of_actual_object;
}
}
So that I can call other subclass methods from subclass reference without casting. Like this:
SubClass obj=new SubClass();
obj.someMethodThatDoesSameThingForAllSubclasses().someSubclassMethod();
//currently this gives compiler error. because first method returns superclass reference and super class doesn't have someSubclassMethod
Is this posible and does it make sense to try to do something like this?