0

I wish to let the withParameters(String token, RiskRating details) method take in an object of type that's either a RiskRating object or is a subclass of it

Something like withParameters(String token, T extends RiskRating details)

public Response withParameters(String token, RiskRating details)  {
        return SerenityRest.given()
                .header("Accept", "application/json")
                .header("Content-Type", "application/json")
                .header("Authorization", "Token " + token)
                .body(details)
                .post(WebServiceEndPoints.RISKRATING.getUrl());

    }

How do I achieve this?

  • `T extends RiskRating` is already correct, you just need to place it correctly. See the given link how to do so for methods. – Tom Dec 21 '20 at 09:28
  • `public Response withParameters(String token, T details) {}` – Hadi J Dec 21 '20 at 09:30
  • 1
    Your method does already “take in an object of type that's either a RiskRating object or is a subclass of it”. – Holger Dec 21 '20 at 11:25

1 Answers1

1

There is no need for generics here.

Passing an instance of a subclass to a method which takes a parent class is fine.

A subclass IS-A superclass.

public class Animal {}
class SubAnimal extends Animal {}

class Example {
    static void takesAnimal(Animal a) {}
    static void passesSubAnimal() {
        takesAnimal(new SubAnimal());
    }
}
tgdavies
  • 10,307
  • 4
  • 35
  • 40