0

We're in a situation where we have a class "Base", which has a final toString() method.

We also have a final class "Sub" which extends "Base".

There's a requirement to do a change that's very specific and only applies to a miniscule subset of functionality, but can get pretty intrusive if done "properly".

The easiest way to achieve the change would be to override the toString method on a single instance of the "Sub" class. Creating a new single instance of Sub with the overriden toString() is also an option.

Is this possible to do? Any advice on how to do this?

7
  • " final toString() method" ? And still you plan to override ? Commented Jul 12, 2022 at 10:40
  • Technically you can't. You should ask yourself why exactly is the Base.toString() method declared final. If there is a good reason for that, then you shouldn't try to override it. Perhaps could you provide a sample of code so we can understand better what is at stake here ? Commented Jul 12, 2022 at 10:47
  • Yes, if it was possible to do on a single instance. Yes there might be a good reason why it's final. It's a very generic class, used in many places. I just need this one single instance in one place to return something else for toString() Commented Jul 12, 2022 at 10:48
  • 3
    @TeabagD without some serious tampering of the code, it's not possible. Even with tampering, it's not necessarily possible. Commented Jul 12, 2022 at 10:57
  • 2
    The important question is, what do you want to achieve with the overridden toString() method? Also worth reading: What is the XY problem? Commented Jul 13, 2022 at 14:55

2 Answers 2

1

If you can't change Base class, to remove the final modifier or change the toString implementation to delegate to child class, your best bet would be to add and use interface: have Base and Sub implements the same interface, use the interface everywhere, then do what you need to do using composition:

class Delegate implements CommonInterface { private Base delegate; // ++ ctor public String toString() { return delegate.toString(); // here you are free to change the toString. } } 
Sign up to request clarification or add additional context in comments.

Comments

0

If any method in java is final, the child classes cannot override the final method. There is only one way if you insist on using toString() in your Sub. Make the toString() of Base class not final.

or

Take a non static final global variable in your Sub class and initialize it using constructor with the required fields to be printed whenever an instance of Sub class need to be printed.

4 Comments

"in your sub class", first of all, the base class is already final, there is no subclass. Secondly, unless you override the toString method, adding an instance variable in a subclass (which you don't have) won't alter the functionality of the toString method
@Stultuske That's not what the OP said : the Base class is not final, only its toString() method is.
@Stultuske I know the variable will not alter the functionality of toString(). The Base class is not final but the Sub class is final which extends the Base class. The toString() in Base class is final. Please read the problem once agian.
@nquincampoix I was mistaken based on the title

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.