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?

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

1 Answer 1

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());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.