简体   繁体   中英

How to return a static instance in subclass type

I am currently two class. Response with a simple attribute "responseCode". Response has a static instance (A simple response when it is KO).

I have also a class DetailResponse which is a subclass of my class Response.

What I would do is to be able to return an object of type DetailResponse using my static variable KO_RESPONSE in Response class. Is there a way to do it simply ?

My super class Response :

public class Response {
    public static final Response KO_RESPONSE = new A(ReturnCode.KO);

    public ReturnCode responseCode;
    public String comment;

    public Response () {};
    public Response (ReturnCode responseCode) {
        this.responseCode = responseCode;
    }

    public static enum ReturnCode {
        OK,
        KO;
    }
}

My subclass DetailResponse which extends Response :

public class DetailResponse extends Response {
    public String otherField;
}

BuisinessService class :

public BuisinessService {
    public DetailResponse sendRequest() {
        String status = sendRequest() // Do something
        if(status.equals("KO")) {
            return Response.KO_RESPONSE; // What I would do but doesn't work because Response is the super class of DetailResponse
        } else {
            DetailResponse detail = new DetailResponse(ReturnCode.OK);
            detail.comment = "comment";
            detail.otherField = "somethingCool";
            return detail;
        }
    }
}

You are trying to override static variables, Java doesn't allow this. Instead you can implement below pattern to get instances of your base and child class. Here is an example:

    public class Response {
     public static Response getInstance(){
      return new Response(<args>);
     }
    }

    public class DetailedResponse extends Response {
     public static DetailedResponse getInstance(){
      return new DetailedResponse(<args>);
     }
    }

Finally the Business Class can be implemented as below:

public BuisinessService {
    public DetailResponse sendRequest() {
        String status = sendRequest() // Do something
        if(status.equals("KO")) {
            return DetailResponse.getInstance(); // What I would do but doesn't work because Response is the super class of DetailResponse
        } else {
            DetailResponse detail = new DetailResponse(ReturnCode.OK);
            detail.comment = "comment";
            detail.otherField = "somethingCool";
            return detail;
        }
    }
}

Hope this helps!

Static variables and method are not inherited, but you can use a static block initializer

public class Response {
        public static Response KO_RESPONSE;

        public ReturnCode responseCode;
        public String comment;

        public Response () {};
        public Response (ReturnCode responseCode) {
            this.responseCode = responseCode;
        }

        public static enum ReturnCode {
            OK,
            KO;
        }
    }

    public class DetailResponse extends Response {

        static {
            Response.KO_RESPONSE = new DetailResponse(/* .. */);
        }

        public String otherField;
    }

Why you have used A() here, A is not defined.

public static final Response KO_RESPONSE = new A(ReturnCode.KO);

Detailed response or other type of response can be used here

class DetailResponse extends Response {

    public DetailResponse(ReturnCode ko) {
        super(ko);
    }

    public String otherField;
}

class Response {
    public static final Response KO_RESPONSE = new Response(ReturnCode.KO);

    public ReturnCode responseCode;
    public String comment;

    public Response () {};
    public Response (ReturnCode responseCode) {
        this.responseCode = responseCode;
    }

    public static enum ReturnCode {
        OK,
        KO;
    }
}

Also you must have to declare a parameterized constructor for your detailed response class that can be used to defined

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM