About the subinterface Java subtype

This seems like a major Java issue.

I have one interface Pipeline, which has a method execute(Stage).

Then I create an additional interface for the extension from Pipeline, say BookPipeline, I like the method execute(BookStage).

BookStagecontinues from Stage.

It seems that such a definition cannot pass java compilation.

Any suggestion on this?

+5
source share
3 answers

You might want to consider using generics.

public interface Pipeline<T extends Stage> {
     public void execute(T stage);
}

public interface BookPipeline extends Pipeline<BookStage> {
     @Override
     public void execute(BookStage stage);
}
+6
source

In addition to what @Jeffrey wrote as a possible solution, it is important to understand why you cannot do this.

, Pipeline execute(Stage) BookPipeline execute(BookStage).

, Conc, BookPipeline.

Pipeline p = new Conc();
p.execute(new Stage());

? !
Java , , .

/ , .

+4

@amit, , Conc.execute BookStage , Stage (, , Stage BookStage s).

, , BookePipeline.execute - Stage, Object.

, :

interface Pipeline
{
  void execute(Stage s);
}

interface BookPipeline extends Pipeline
{
  @Override
  void execute(Object s);
}

Conc BookPipeline:

Pipeline p = new Conc();
p.execute(new Stage());

, Liskov Substitutability - Stage , Stage . . Java , .

, ( , , Eiffel, ).

Java . , Pipeline

Stage getAStage();

BookPipeline :

@Override
BookStage getAStage();

, :

public void someMethodSomewhere(Pipeline p)
{
  Stage s = p.getAStage();
  //do some dance on Stage
}

, Donc, Pipeline overrode getAStage() , Pipeline ( Stage), :

someMethodSomewhere(new Conc());
someMethodSomewhere(new Donc());

Stage - (, BookStage) Stage.

, , , /, , , , , . ( Java .)

, PECS - , (Joshua Bloch, Java)

0
source

All Articles