Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Inheritence or static method

There is an abstract class called AbstractAgent and currently there are 27 classes who are extending this class.

I have started developing some agents and all my 5 agents are extending AbstractAgent class.

Now I observed that there is a getFilePath() which I am copy-pasting in all my 5 agents. Then I realized that out of existing 27 classes also there were many classes who had this method. I thought it would be a good idea to put this method in the base class AbstractAgent and let everybody use this method. But I dont want all existing classes to change their code I have changed the method name so that going forward anyone can use it.

The person who did my code review suggested me not to touch AbstractAgent class because it is already being used by existing clients and put this method is some utility class.

I am not convinced by his argument. Anybody want to pitch in their thoughts.

like image 737
user977263 Avatar asked Sep 11 '26 15:09

user977263


1 Answers

In this case because the AbstractAgent class is already being used by other classes, and as you mentioned yourself NOT all the classes implement this method then you should indeed NOT change the AbstractAgent. You can however extend AbstractAgent in another abstract subclass called say AbstractAgentWithFilePath, which declares that additional method you want, and then have those classes extend the AbstractAgentWithFilePath class instead.

like image 60
Oscar Gomez Avatar answered Sep 13 '26 04:09

Oscar Gomez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!