Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract method implementation that does nothing

I'm not quite sure how to word this question, but hopefully the example will make it a bit more clear. I'm trying to figure out the best way to have one of the implemented abstract methods not be called (do nothing) and I'm curious if my current approach is at all somewhat right.

abstract class Vehicle {
  void doSomething() {
    if (this.getClass() instanceof Chevy) {
      operateOnCar();
    }
  }
  abstract void operateOnCar();
}

class Chevy extends Vehicle {
  @Override
  void operateOnCar() {
    print("Doing something to a car")
  }
}

class HarleyDavidson extends Vehicle {
  @Override
  void operateOnCar() {
    throw Exception("This isn't a car")
  }
}

The other approach I can think of is to have the HarleyDavidson class implement operateOnCar()but do absolutely nothing - i.e. an empty method body. Is that potentially better? Maybe neither of these are viable examples and I should reconsider my design. Thanks!

Edit: tried to make my example a bit more clear

like image 329
joshft91 Avatar asked Mar 08 '26 02:03

joshft91


2 Answers

I'm trying to figure out the best way to have one of the implemented abstract methods not be called (do nothing)

Asserting that a method should not be called is totally different from asserting that it should do nothing. It is furthermore wrongheaded to define a method on the superclass, regardless of abstractness, that is not ok to call on any instance of any subclass. Thus, some variation on the "do nothing" alternative is a much better choice.

And what's so hard about a method doing nothing when it does not need even to provide a return value? This is a method that does nothing:

void isCar() {
    // empty
}

I should also observe at this point a method named isCar would be expected by most Java programmers to return a boolean indicating whether the object on which it is invoked is a "car", whatever that means in context. It would come as a surprise that such a method is declared not to return anything, and perhaps an even bigger surprise that it writes to System.out.

like image 152
John Bollinger Avatar answered Mar 09 '26 18:03

John Bollinger


You're blurring the responsibilities of your abstract class and its concrete implementations. This is evidenced by your doSomething method.

void doSomething() {
    if (this.getClass() instanceof Chevy) {
      operateOnCar();
    }
}

Your abstract class shouldn't care what instance it is; only the Chevy class needs to do something with this.

This may be why you're getting mixed up with the operateOnCar method. A Car is a Vehicle, but not all Vehicles are Cars. You could have trucks, vans, locomotives, boats, planes...all of which are vehicles in their own right, but definitely wouldn't support an operateOnCar method.

It may be as simple as renaming your method. You can definitely operate or fix a vehicle. You just want to keep that as agnostic as possible at the higher levels of the inheritance chain.

like image 24
Makoto Avatar answered Mar 09 '26 16:03

Makoto



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!