EDIT:
To be clear: The fact that the design is quite ugly is not the point. The point is, that the design is there and I am in the situation to have to add another sub-class of FlyingMotorizedVehicle
which would not work as expected if I forgot to add the foo(...)
. So I just was wondering if I could redefine it as abstract.
I am right now facing a quite weird inheritance situation.
Lets say, I have three classes, Vehicle
, MotorizedVehicle
and FlyingMotorizedVehicle
as well as a bunch of classes Airplane
, Helicopter
, ...:
public abstract class Vehicle {
abstract Something doStuff(...);
abstract SomethingElse doOtherStuff(...);
abstract Foo bar(...);
}
public class MotorizedVehicle extends Vehicle {
@Override
Something doStuff(...) {
return new Something();
}
@Override
SomethingElse doOtherStuff(...) {
return new SomethingElse();
}
@Override
Foo bar(...) {
return new Foo();
}
}
public class FlyingMotorizedVehicle extends MotorizedVehicle {
@Override
SomethingElse doOtherStuff(...) {
return new SomethingElse();
}
}
public class Airplane extends FlyingMotorizedVehicle {
@Override
Foo bar(...) {
//do something different
return new Foo();
}
}
public class Helicopter extends FlyingMotorizedVehicle {
@Override
Foo bar(...) {
//do something totally different
return new Foo();
}
}
[...]
So Vehicle
is an abstract class providing some abstract methods. MotorizedVehicle
is a sub-class of Vehicle
with concrete implementations of its methods. FlyingMotorizedVehicle
again is a sub-class of MotorizedVehicle
overriding the implementations of a subset of MotorizedVehicle
s methods.
Now there are the sub-classes Helicopter
, Airplane
and potentially some others which in the example override the concrete implemenatation of MotorizedVehicle#bar(...)
.
What I want is to "force" every sub-class of MotorizedVehicle
to have to override the bar(...)
method and provide its own implementation.
Is it possible to just change the FlyingMotorizedVehicle
in the following way:
public class FlyingMotorizedVehicle extends MotorizedVehicle {
@Override
SomethingElse doOtherStuff(...) {
return new SomethingElse();
}
abstract Foo bar(...);
}
So that I just redefine the bar(...)
as abstract method? My IDE is not complaining about it, but that of course does not mean, that it will actually work.
I hope you get what I try to point out here.
Thanks in advance
Bluddy