how to override methods with generic return type in java in case of multi level inheritance

bushra

I have an abstract class containing an abstract method which returns a generic type. lets say

abstract class AbstractSample{
  //class members

  //this method needs to be overriden
  public abstract SampleBuilder<? extends AbstractSample,?> copyBuilder ();

  //other methods
  //builder
  public static abstract class SampleBuilder<T extends AbstractSample, B extends SampleBuilder<T,B>> {
    //properties
    private String as;

    public SampleBuilder(AbstractSample object) {
      this.as = object.as;
    }

    public B as(String as) {
      this.as = as;
      return (B)this;
    }

    public abstract T build(); 
  }
}

Now I have a class Sample1 which extends this class and implements the method copyBuilder()

public class Sample1 extends AbstractSample {
  //class members

  @Override
  public AbstractSample.SampleBuilder<Sample1,Sample1.Builder> copyBuilder()      {
    return new Sample1.Builder(this);
  }

  //other methods
  //builder
  public static class Builder extends SampleBuilder<Sample1,Builder> {
    //implementation
    //containing members and methods
  }
}

There is another class Sample2 which is a subclass of Sample1. When I try to override the method copyBuilder() in this class, I am getting issues in the return type.

class Sample2 extends Sample1 {
  //class members

  // I am not allowed to implement this as below
  @Override
  public AbstractSample.SampleBuilder<Sample2,Sample2.Builder> copyBuilder() { 
    return new Sample2.Builder(this) ; 
  }

  //other methods
  //builder
  public static class Builder extends SampleBuilder<Sample2,Builder> {
    //implementation
    //containing members and methods
  }
}

IntelliJ says the method clashes with the one in the superclass , attempting to use incompatible return type.

Could anyone suggest how to implement the method copyBuilder() in Sample2 so that it returns Sample2.Builder .

Rafał Spryszyński

I don't like a part B extends SampleBuilder<T, B>. Why is it even useful to reference itself?

Anyway You can make it work if You change

SampleBuilder<Sample1,Sample1.Builder> copyBuilder()

to

SampleBuilder<? extends Sample1, ?> copyBuilder()

A complete example is below

public class Test {
abstract static class AbstractSample {

    private String as;

    //this method needs to be overriden
    public abstract SampleBuilder<? extends AbstractSample, ?> copyBuilder();

    public static abstract class SampleBuilder<T extends AbstractSample, B extends SampleBuilder<T, B>> {

        private String as;

        public SampleBuilder(AbstractSample object) {
            this.as = object.as;
        }

        public B as(String as) {
            this.as = as;
            return (B) this;
        }

        public abstract T build();
    }
}

static class Sample1 extends AbstractSample {

    @Override
    public SampleBuilder<? extends Sample1, ?> copyBuilder() {
        return new Builder(this);
    }

    public static class Builder extends SampleBuilder<Sample1, Builder> {

        public Builder(Sample1 sample1) {
            super(sample1);
        }

        @Override
        public Sample1 build() {
            return null;
        }
    }
}

static class Sample2 extends Sample1 {

    @Override
    public SampleBuilder<? extends Sample2, ?> copyBuilder() {
        return new Builder(this);
    }

    public static class Builder extends SampleBuilder<Sample2, Builder> {

        public Builder(Sample2 sample2) {
            super(sample2);
        }

        @Override
        public Sample2 build() {
            return null;
        }
    }
}
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Return Type of Java Generic Methods

From Java

Java wildcard in multi-level generic type

From Dev

How to apply generic constraint to accept multi level of inheritance C#

From Dev

Java generic methods, wildcard List return type

From Dev

Java Generic type inheritance

From Dev

Kotlin - Return Type For Generic Inheritance

From Java

Java generic function: how to return Generic type

From Dev

How to override abstract generic method with concrete return type?

From Java

Java generic method inheritance and override rules

From Java

Java Object return type vs. Generic Methods

From Java

JAVA: generic type class inheritance and generic type inheritance

From Dev

Multi-level generic type constraints in Swift

From Java

Java generic return type

From Dev

Java return type generic

From Java

Generic Methods and Type Inferencing in Java

From Dev

Override return type of subclass on Java

From Java

Java generic type not working in this case

From Dev

Multi level inheritance in PHP

From Dev

Python: multi level inheritance

From Dev

Kotlin generic methods and inheritance

From Dev

Unbound generic return type in Java

From Java

Java weird generic return type

From Java

Return type of generic method (Java)

From Dev

Java Generic Interface Return Type

From Dev

java generic method return type

From Dev

How to override different types of generic type

From Java

How to have Java method return generic list of any type?

From Java

How to return the Class of a generic type

From Dev

How to set a generic return type