Implement only direct methods from an interface

Asked

Viewed 54 times

3

Let’s say I have the interface Vehicle:

public interface Vehicle {
    void moveForward();
    void moveBackward();
    void moveLeft();
    void moveRight();
}

And the interface LandVehicle:

public interface LandVehicle extends Vehicle{
    void repairTire();
}

If I have a class, say, Car, it is possible that I only implement the interface methods LandVehicle? Having a standard Vehicle implementation in another class?

For every earthly vehicle will have its own execution of the method repairTire(), but all will have the same execution of the methods used for locomotion of the Vehicle interface.

My intention is to make an abstraction of the methods used for the database, so that if one day is necessary I can exchange the database without needing to refactor in the classes that already used these methods. Based on the interface CrudRepository spring (I cannot use spring in this project). I would have for example:

Usuariorepository <- Mongorespository <- Crudrepository

  • Is there a reason not to use default methods on the interface or then use to transform the interface into a superclass? If yes which?

  • Your problem reminds me of this: https://pt.meta.stackoverflow.com/q/499/132

1 answer

3


it is possible that I only implement the interface methods LandVehicle?

It’s not possible, you have to implement everything, somewhere. Nor would it make sense to allow it. An interface is a contract, when settling for it in its class means that it wants to fulfill all the contract it requires. If you don’t want to do this then you don’t want to conform to this interface.

It may be that the interfaces were poorly thought out and wanted to assemble in another way, it may be that there is a lack of greater segregation of the interfaces and only have in them what matters for each of these interfaces, so it is mixing responsibilities. If you have the right separation you could choose the right interface to conform and not gain unwanted behaviors in the object. So the inheritance there is wrong. It violates one solid principle behind each other without a plausible reason.

Having a standard implementation of Vehicle, in another class?

The code does not show a standard implementation. If you have one then every time you access this default implementation will be used and then you don’t need to implement again, but it’s not that you don’t need to implement, it’s already implemented.

  • 2

    It may be that AP is considering creating a class that implements Vehicle and implements each of the interface methods, and create a subclass that implements LandVehicle, there would not need to implement those that have already been implemented.

  • It may be, but it would need to be clearer. Anyway it’s worth the same as I said for the interface to already implement. , what is already implemented does not need to implement again, and there is no problem whatsoever.

  • @Piovezan was exactly that, I hadn’t thought of it that way. A class that implements Vehicle and a class that extends this implementation and also implements Landvehicle methods.

  • 1

    And it’s really looking like an XY problem

Browser other questions tagged

You are not signed in. Login or sign up in order to post.