-2

I had a scenario like below

interace A {

}

class B implements A {
}

Now during making changes, I realized that I have to extract out a repeating piece of code into a method.

say there is a method like below in class B:

private void domSomething() {
 //sequence of steps
}

Now, the issue is that in future we expect other implementations of interface A to use method doSomething().

So the dilemma here is should method doSomething() be moved to a util class or we should create an abstract class with a protected doSomething() method. Something like below.

abstract class C implements A {
 protected void doSomething();
}

class B extends C {

}

Or create a Utils class and let B to still implement A.

Generally, I like to refrain from using abstract and look for ways to avoid them. So that leads to some questions here:

  1. Is that right way to think about abstract classes?
  2. Which approach should be more preferred here and why?
  3. Any other suggestion behind the thought process is always welcomed.
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Global Warrior
  • 5,050
  • 9
  • 45
  • 75
  • if i get you question right, you should write you` doSomething()` method into a class , and to answer to you question is no is not the right way to think about the abstract class , if you want you have to change a class to abstract and implement `doSomething()` function inside of it so if one of the child class don't implement this function , when this function been called your code run the parent function , to answer to your second question i should say move function to a class . – Rome Jul 29 '17 at 07:05

3 Answers3

3

I would prefer going for "util class" (but not static! Just another class defining a common behaviour). In another words prefer "composition over inheritance".

More info why here:

Prefer composition over inheritance?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jan Muncinsky
  • 4,282
  • 4
  • 22
  • 40
0

private void domSomething() does not return value, that mean method update some state, it can be global state (database or external resources) or it can be internal state of class this method exists in.

If method updates global state based on internal members of class, then it should be moved to another class and all required data passed in as parameter

public class Util
{
    public void DoSomething(value1, value2) {}
}

If method updates internal state of class then it can be placed to abstract class, so all derived classes have access to it.

public abstract class A
{
    protected void DoSomething() {}
}

Make method virtual/overridable if you want give derived classes possibility to change it's behaviour.

Fabio
  • 31,528
  • 4
  • 33
  • 72
0

Use abstract classes when you are modeling some hierarchy in your classes, move common logic to parents, more over try to prefer composition over inheritance

And use static util/helper classes when logic is very generic and can be used by multiple classes, e.g FileUtils.createTmpFile

sagarr
  • 1,152
  • 1
  • 9
  • 16
  • Using static method is something that should be avoided at the first place! In OOP there is no place for such a design. – Jan Muncinsky Jul 29 '17 at 07:23