0

I am newbiew in Java programming. I've stuck on streams learning at this moment. Please, review two code areas below, which are parts of streams creation:

  1. for Sink interface (extends from interface java/util/function/Consumer)/ nested class ChainedReference
abstract static class ChainedReference<T, E_OUT> implements Sink<T> {
    protected final Sink<? super E_OUT> downstream;

    public ChainedReference(Sink<? super E_OUT> downstream) {
        this.downstream = Objects.requireNonNull(downstream);
    }
    ...
}
  1. for abstract class ReferencePipeline (extends from AbstractPipeline)/ opWrapSink method
@Override
Sink<P_OUT> opWrapSink(int flags, Sink<P_OUT> sink) {
    **return new Sink.ChainedReference**<P_OUT, P_OUT>(sink) {
        @Override
        public void begin(long size) {...}

        @Override
        public void accept(P_OUT u) {...}
    };
}

Could you advice how operator "new" works in code "return new Sink.ChainedReference"? From one hand, abstract classes cannot have instances. From another hand, we use operator "new" to create a class instance. In curly brackets, code authors override methods and sometimes add new variables. Can we use operator "new" with abstract static class constructor to override methods and define new variables?

Best Regards

I've expected that operator "new" couldn't be used with abstract classes

  • 1
    See also https://stackoverflow.com/q/18704298/869736 and https://stackoverflow.com/q/37773920/869736. – Louis Wasserman Nov 29 '22 at 19:39
  • *abstract classes cannot have instances* -- false. Interfaces and abstract classes cannot be directly *instantiated*, but if they could not have any instances at all then what good would they be? An instance of a class that implements interface `I` is an instance of `I`, and an instance of a subclass of abstract class `A` is in instance of `A` as well. You are looking at instantiation of an anonymous inner class extending `Sink.ChainedReference`. – John Bollinger Nov 29 '22 at 19:45
  • Gents, thank you for replies – Sergey Chernov Nov 30 '22 at 20:04

0 Answers0