Let me break it.
public A first(String s){
list.add(s);
return this;
}
The return type of the method is A, therefore, the call new B().first("hi")returns an object of type A. Therefore, when I tried to compile, I expected errors by saying incompatible types.
You could, like label comments, override the method and do the same, but return B:
public B first(String s){
super.first( s );
return this;
}
or even
public B first(String s) {
return (B)super.first(s);
}
Kesheva , , A, B.
B b = new B();
((B)b.first("unu").second()).bfisrt();
, , , .
, .
public abstract class A {
public <Unknown extends A> Unknown first(String s) {
System.out.println("in here");
return (Unknown)this;
}
}
public class B extends A { }
public static void main(String[] args) {
B b = new B().first("hi").first("hello");
}
fooobar.com/questions/1549886/... , .
:, newacct, , , , . :
B b = new B().first("hi").first("hello");
class C extends A { }
C c = new B().first("hi");