At first glance, I would say that it would be better to make common code as a public static method in a public class. This will make the method useful for any class simply by using
UtilityClassName.methodName();
This is better than making a specific abstract superclass method, because then you always need to extend that superclass in all classes where you want to use this single method.
But now, as you said, the behavior of a method depends on some variables. Now, if it depends on the instance variables of different classes, it is better to add this method to the interface and let all your classes implement this interface and have their own implementation of the same.
But then again, if these variables are constant values, then these constant values ββare in the interface. Deploy this interface in your class. And again, make it a static method in this utility class that will directly use these constants.
For example, consider foll. common code of the returned area of ββthe circle.
public interface TwoDimensional{ double PI = 3.14; } public class MyUtility implements TwoDimensional{ public static double getCircleArea(double radius){ return PI*radius*radius; } }
Here you can see that the getCircleArea () method depends on the radius, which will differ for different classes, but still I can pass this value to the static method of the myUtility class.
whitehat
source share