In the Java interface, how can I * * not use one specific method inherited from the parent interface?

I have a hierarchy of three interfaces, grandparent, parent and child. Parent and child have an add method, which requires different input parameters for the child. As long as there is no problem adding the required signature to the child process, the inherited method will be pointless , so is there a way to not have it there at all? Other methods work fine.

Perhaps in order to achieve what I want, I can completely improve the design, so I will briefly talk about what interfaces are:

I collect the counters, which consist of time and value. The grandparent interface is single-read. I also have classes that are a series of consecutive readings (series) and one that contains several series working over the same period of time (just call this table).

A table can be viewed as a series (which aggregates values ​​orthogonally to the time axis), and both tables and series can be considered as one reading (implementations that provide different methods of aggregation), therefore, inheritance. This seems to work fine, but for the add method. (I can add one point to the series, but for the table, I need an additional parameter to tell me which series it belongs to.)

+3
source share
7 answers

It might be wise to break interface inheritance all together. Just use special interfaces for certain types of behavior. No matter what classes you have that implement these interfaces, you can simply choose the ones that make sense and don't have to worry about implementing methods that don't make sense.

+6
source

No, you cannot avoid the inheritance of the method, as this violates the principle of Liskov replacement .

In practice, you could implement implementations UnsupportedOperationException, but that would be pretty unpleasant.

Can't you implement an inherited method with some kind of default value for the series?

+9
source

, , .

B A, , B A. OOP, -, , .

, B A, , , :

BAD. "Unimplemented", . , , .

. , B A (, / ), , . , .

+4

, , , . , , , :

​​, , , , , , .

, - .

+1
+1

- . , , , . ( , , ), , , .

0

, :

class A implements B{ void add(A) { /*Goes Nowhere Does Nothing*/ return;} }

. , - hasParent(): boolean hasChild(): boolean. , liskov, .

0

Source: https://habr.com/ru/post/1706140/


All Articles