Home > OS >  Overload Constructor
Overload Constructor

Time:04-21

I have this MyClass that has a 3 fields. But I need to change the datatype info into an object of Info, to accomodate 2 more fields. The String info, will be equivalent to info.text. But there is a lot if calls on the MyClass Constructor and we dont want to change it as the only required field is the info.text.. How can I overload the constructor? For some will call the new Constructor with an argument of object Info instead of String.

public class MyClass {
        
       private String status;
       private String reason;
       private String info;

        public MyClass(final String status, final String reason, final String info) {                                  
                this.status= status;
                this.reason= reason;
                this.info= StringUtils.trimToNull(info);
        }

}

@Getter
@Setter
public class Info {

   private String text;
   private String image;
   private String details;

}

CodePudding user response:

If I get you right, you probably want something like:

public class MyClass {       
   private String status;
   private String reason;
   private Info info;

    public MyClass(final String status, final String reason, final Info info) {                                  
            this.status= status;
            this.reason= reason;
            this.info = info;
    }

and

    public MyClass(final String status, final String reason, final String infoText) {                                  
            this(status, reason, new Info().setText(StringUtils.trimToNull(infoText));
    }

Meaning: you change your class to have an Info object, instead of an "Info" string. Then you allow to create your class by passing an Info object; but you keep the existing constructor, but you invoke the new one, and pass in an Info object that gets set up with the text property.

Of course that only works when your setters return the object they are setting. In case your setters have return type void, that "trick" to invoke the other constructor isn't possible, in that case you will end up with two very similar but slightly different bodies for the constructors.

CodePudding user response:

Expanding on GhostCat's answer. The way I understood it, you need to keep old constructor, getters and setters to support Legacy. You can replace the String info field, but still need its setter and getter. You can overload the setInfo() method and add a new getter for the new field. Also, the Info class needs to override the toString() method in the Info class to return a String formatted the same way that legacy code expects it.

public class MyClass {       
   private String status;
   private String reason;
   private Info info;

   // constructors

   public void setInfo(String info) {
      // convert String to Info and set in field
   }

   public String getInfo () {
      return info.toString(); // need to override toString() in Info class
   }

   // new getter and setter
   // Overloaded method (different param list)
   public void setInfo (Info info) {
      this.info = info;
   }

   // cannot use same name because param list is the same
   public Info getInfo2() {
      return info;
   }
}
  •  Tags:  
  • java
  • Related