Can you re-make a method abstract in the inheritance tree?

后端 未结 4 589
春和景丽
春和景丽 2021-01-25 03:09

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-c

4条回答
  •  清歌不尽
    2021-01-25 03:35

    Brother, study Interfaces too.

    If you want some class, such as Vehicle to only provide function prototypes, then you should always use Interface.

    And make your FlyingMotorizedVehicle as abstract class.

    • Abstract class can have both types of functions, either prototype only (abstrac functions) or fully implemented functions.
    • Interfaces have only function prototypes, they can't contain function implementations, Interfaces are required to be implemented.

    For further study, you can find many useful links, including this one.

    =============================CODE-EXAMPLE=======================================

    For Vehicle

    public interface Vehicle {
    
        Something doStuff(...);   
        SomethingElse doOtherStuff(...);
        Foo bar(...);
    
    }
    

    For FlyingMotorizedVehicle

    public abstract class FlyingMotorizedVehicle extends MotorizedVehicle {
    
        SomethingElse doOtherStuff(...) {
            return new SomethingElse();
        } 
    }
    

    ===============================================================================

    Happy OOP-ing!

提交回复
热议问题