I have the following situation in java; There is a class that implements a method that is meant to be final but I need the returning type of such method to be exactly that of the class that it calling it. Since I want the method to be final I can't modify the return data type in the subclasses since that would be illegal.
Is there a way to archive this without sacrificing finality and having to override such method for all subclasses that implement it?. Even if the method was not final it would still be faster to implement it this way.
The code would be something like this:
class Parent
{
public currentClass finalMethod() {...}
}
class Children extends Parent {}
public static void main(String args[])
{
Children c = new Children();
System.out.print(c.finalMethod().getClass().getName()); // Would print Children
}
Tried to use reflection and generics to no avail. Examples:
class Parent
{
public this.getClass() finalMethod() {...} //Many Errors
public <extends Parent> finalMethod() {...} // Returns Parent even if called by Child
public Class<? extends Parent>[] getVecinos() // Returns Class<? extends Parent> thus can't use polymorphism which is the use case
}
I know I could use casting but still not the best solution.