1

I have below classes.

public class BaseRequest {
    protected List<Emp> names;  // names property is required in many classes 
}

public class FirstRequest extends BaseRequest {

    public FirstRequest() {
        names = new ArrayList<Emp>();
    }

    public setNames(List<Emp> names){
        super.names = names;
    }

    public List<Emp> getNames(){
        return super.names;
    }
}

public class ServiceClass {

    public void someMethod(List<Emp> emps) {
        FirstRequest request = new FirstRequest();
        request.setNames(emps);
        //Some Logic
    }
}

Am i doing inheritance in right way? how i can i improve it further?

Thanks!

5
  • 2
    use BaseRequest request = new FirstRequest(); instead of FirstRequest request in side the ServiceClass Commented Dec 11, 2013 at 12:21
  • @SantoshJoshi then he needs to declare the getters and setters on the superclass also, while it could be abstract and implement them. Commented Dec 11, 2013 at 12:22
  • 2
    Why is names defined in BaseRequest but the setter and getter as well as the initialization in FirstRequest? There might be reasons for this but unless it's on purpose, move that code to BaseRequest as well. Commented Dec 11, 2013 at 12:23
  • Looks okay, but we do not have any context. Commented Dec 11, 2013 at 12:27
  • If you have a protected Attribute in in your super-class every other class in the same package can directly access it. This is uncommon and I would recommend not to do that. In the consequence you would need to move the getters / setters and the Contructor that initialises the NamesList to the Superclass. That would lead to a common pattern. Commented Dec 11, 2013 at 12:34

3 Answers 3

1

How i can i improve it further?

Move the methods related to names into the base class, and make names private.

public class BaseRequest {
    private List<Emp> names = new ArrayList<Emp>();

    public setNames(List<Emp> names){
        this.names = names;
    }

    public List<Emp> getNames(){
        return names;
    }
}

public class FirstRequest extends BaseRequest {
    // the rest of your stuff
}

You should avoid making names protected: a public getter should be good enough for derived classes and for the other users of the class.

Sign up to request clarification or add additional context in comments.

Comments

0

Depends on what you want to do, for this piece of code most things seem okay. Might want to change

FirstRequest request = new FirstRequest();

to

BaseRequest request = new FirstRequest();

And use getters / setter in the superclass.

Also, in the constructor of the FirstRequest, you should talk to the super class (do the ArrayList initialisation there for example)

 public FirstRequest(){
 super();
 }

and in the super class

public BaseRequest()
{
// initialisation
}

Comments

0

You can convert the BaseRequest to an interface and provide only the signatures of the methods to be used by the clients. The client should be interested only to the methods provided (api), not the implementation details Then you can implement them in all classes implement the interface. It is more flexible with an interface:

public interface BaseRequest {
    List<Emp> getEmps();
    void setEmps(List<Emp> list);
    ....
}


public class FirstRequest implements BaseRequest{

    List<Emp> getEmps(){
        return ...;
    }

   ....
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.