Question

I have below java abstract class.

public abstract class Base implements Serializable {


    private static final long serialVersionUID = 2602178514139825116L;

    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }


}

MainRequest.java

public class MainRequest extends Base {

    te static final long serialVersionUID = -1724534962722346709L;


    private String key;
    private String name;


    public MainRequest(String id){
        super.setId(id);
    }

    public MainRequest(String key, String name){
        this.key = key;
        this.name = name;
    }

    public String getKey() {
        return key;
    }


    public String getName() {
        return name;
    }


}

Is my MainRequest .java class correct? Do i need to call super class constructor also? Any improvements to MainRequest.java ?

Thanks!

Was it helpful?

Solution 3

Improvements would be:

  • implement a constructor like Base(String id) and call it from your MainRequest class, so every subclass of Base must have an id value.

    public Base(String id) {
        this.id = id;
    }
    
  • consider using final on the id member variable in Base class and remove the setId method. In most cases you don't really want to set an id twice, do you?

    private final String id;
    
  • make a constructor like MainRequest(String id, String key, String name) to set all things at once like:

    public MainRequest(String id, String key, String name) {
        super(id);
        this.key = key;
        this.name = name;
    }
    
  • the use of an abstract class is fine, since you're implementing the getId method and having a constructor but not want to instantiate this base class at all.
  • possible add final key word on the key and name member, if you only want to set these variables once in the constructor (assuming that, because you already have no setter methods):

    private final String key;
    private final String name;
    
  • post working code in your question (see te => private)

  • put the question to https://codereview.stackexchange.com/ :)

Of course, some points depending on what you really want to do! But you didn't tell some details about your intention!

OTHER TIPS

For this kind of a case Base no need to be abstract class it is enough to use an interface.

Inside MainRequest(String key, String name) constructor, no need to use super() constructor. You did not use default constructor MainRequest(){ }, so it's jre's responsibility to provide super() inside the parameterized constructor. You used serializable interface, it has no abstruct method, so while implementing this interface, no headache to define the abstruct methods of that interface. because, it has nothing. (It is called markup interface, aimed to accomplish certain task while IO operation). If you dont pass id value in the parametric constructor, default value of int will be taken by jre.default value is 0. any issues ?

One thing seems strange. If your supper-class has id property, it seems it is usefall for all chiles. So it's not good create a child class without it. So add this to other constructor too:

public MainRequest(String id, String key, String name){
    super.setId(id);
    this.key = key;
    this.name = name;
}

Why did you make Class Base as abstract? As you don't have any abstract method no need for the class to be abstract.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top