Java Abstract classes: returning "this" pointer for derived classes

I am trying to write some custom exceptions using helper methods to set such variables:

public class KeyException extends RuntimeException { protected String Id; protected KeyException(String message) { super(message); } protected KeyException(String message, Throwable cause) { super(message, cause); } public String getId() { return keyId; } public KeyException withId(final String Id) { this.Id = Id; return this; } } 

However, in my derived classes, I cannot use the "withId" method, since it only returns the base class, anyway, to return the "this" pointer without overriding the method in each derived class?

+7
java design-patterns
source share
6 answers

anyway, to return the "this" pointer without having to override the method in each individual derived class?

Yes, look at option 1 below.

Here you can do several ways:

  • Passing a result to a derived class

  • Discard it in subclasses

  • Change the return type to void. Since you are calling a method on an object, you already have a pointer to it.

+8
source share

You can do something like this:

 public <T extends KeyException> T withId(final String Id) { this.Id = Id; return (T)this; } 

Then in the derived class, you simply pass its type as a type parameter.

However, there may be design supervision. In addition to the builder drawing, I rarely see setters returning a link to the object itself. If you provide more context, we can help you.

+7
source share

This is possible with generics using the following construction:

 public class ParentType<T extends ParentType<T>> { public T withId(String someId) { /* Insert other code here */ return (T) this; } } public class BranchType<T extends BranchType<T>> extends ParentType<T> {} public final class LeafTypeA extends BranchType<LeafTypeA> {} public final class LeafTypeB extends ParentType<LeafTypeB> {} 

Where BranchType is a class with subclasses, and LeafTypeA, LeafTypeB are classes without subclasses.

This is a slight improvement over the other generic solution, as it prevents:

 public class LeafTypeA extends BranchType<LeafTypeB> {} 

Since this does not satisfy the restrictions of the type parameter.

+2
source share

If you have a derived class like

 public class AnotherException extends KeyException { ... } 

.... then you can use simpy asign with withId ....

 AnotherException a = new AnotherException ("A"); AnotherException b = (AnotherException) a.withId("ID"); 

... because it is basically the same object. You just need to drop it.

+1
source share

There is one way to solve the problem of returning a subclass using generics:

 // base class public class Base<T extends Base> { private T myself; public Base(T myself, Class<T> cls) { this.myself = myself; } public T withSomething() { return myself; } } // subclass public class SomeSubCls extends Base<SomeSubCls> { public SomeSubCls() { super(this, SomeSubCls.class); } } 

Using this template new SomeSubCls().withSomething() returns the object as an instance of a subclass, not the parent.

Solemn statements are used , for example, this

0
source share

Unfortunately not. It would be nice if you could do it

 public this withId(final String Id) { // doesn't work this.Id = Id; return this; } 

or simply

 public this withId(final String Id) { // doesn't work either this.Id = Id; } 

or he will know that the methods of "emptiness" are implicitly related (as I believe, one official proposal is proposed)

-one
source share

All Articles