Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: returning subclass in superclass method signature

I'm working on a problem where there are several implementations of Foo, accompanied by several FooBuilder's. While Foo's share several common variables that need to be set, they also have distinct variables that require their respective FooBuilder to implement some specific functionality. For succinctness, I'd like to have the FooBuilder's setters to use method chaining, like:

public abstract class FooBuilder {   ...    public FooBuilder setA(int A) {     this.A = A;     return this;   }    ... } 

and

public class FooImplBuilder extends FooBuilder{   ...   public FooImplBuilder setB(int B) {     this.B = B;     return this;   }   public FooImplBuilder setC(int C) {     this.C = C;     return this;   }   ... } 

And so on, with several different FooBuilder implementations. This technically does everything I want, however, this approach is sensitive to the order of methods calls when method chaining is performed. The following has method undefined compile errors:

someFoo.setA(a).setB(b)... 

Requiring that the developer think about the order of method calls in the chain. To avoid this, I'd like to have the setters in FooBuilder somehow return the actual implementing subclass. However, I'm not sure how to do this. What is the best approach?

like image 686
downer Avatar asked Jun 10 '12 12:06

downer


People also ask

When method in subclass has same name and return type as method in superclass it is known as?

When a method in a subclass has the same name, same parameters or signature, and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class. Method overriding is one of the way by which java achieve Run Time Polymorphism.

Will a method in a subclass with the same signature as a method in a superclass with a different return type cause any problem?

11.14 If a method in a subclass has the same signature as a method in its superclass with a different return type, will this be a problem? It will be a syntax error.

Can we call subclass method using superclass object?

Yes, you can call the methods of the superclass from static methods of the subclass (using the object of subclass or the object of the superclass).

Can subclass access the methods of superclass?

Does a subclass have access to the members of a superclass? No, a superclass has no knowledge of its subclasses.


2 Answers

This is a good question and a real problem.

The easiest way to deal with it in Java likely involves the use of generics, as mentioned in Jochen's answer.

There's a good discussion of the issue and a reasonable solution in this blog entry on Using Inheritance with Fluent Interfaces, which combines generics with the definition of a getThis() method overriden in builder subclasses to solve the problem of always returning a builder of the correct class.

like image 142
Don Roby Avatar answered Sep 18 '22 19:09

Don Roby


Having found this excellent answer I am now sharing it around.

public class SuperClass<I extends SuperClass> {     @SuppressWarnings( "unchecked" ) // If you're annoyed by Lint.     public I doStuff( Object withThings )     {         // Do stuff with things.         return (I)this ; // Will always cast to the subclass. Causes the Lint warning.     } }  public class ImplementationOne extends SuperClass<ImplementationOne> {} // doStuff() will return an instance of ImplementationOne  public class ImplementationTwo extends SuperClass<ImplementationTwo> {} // doStuff() will return an instance of ImplementationTwo 
like image 20
zerobandwidth Avatar answered Sep 22 '22 19:09

zerobandwidth