user1479802
user1479802

Reputation: 211

Protected member Vs private member in inheritance java

I have an abstract class Entity and then multiple instance can extend Entity

like

A extends Entity {
}
B extends Entity {
}

Now all the entity needs to have entityId

So should I have entityId as a private field in Entity and set it via the constructor, or as a protected member in Entity, so that the subclasses can access it directly?

Upvotes: 1

Views: 405

Answers (6)

Phillip
Phillip

Reputation: 1103

Make it a private field in Entity and make (protected) accessors (getter/setter) so that subclasses have access to the field via the setter or getter (basic OO principles)

You can also write a specific constructor for the Entity class, taking an id as argument and call this constructor from the extending classes. By doing so, your subclasses are always forced to set an id.

Eg.

public class Entity {
    private int id;
    public Entity(int id) { 
        setId(id);
    }
    protected int getId() {
        return id;
    }

    protected void setId(int id) {
        this.id = id;
    }
}

public class A extends Entity {

    public A(int id) {
        super(id);
    }
}

Upvotes: 0

MarioDS
MarioDS

Reputation: 13073

This is probably the main reason why protected members exist. Basically it's the same as private but with the exception that it appears public to your subclasses and to classes in the same package (if that's a concern, go with private).

Now, that's the general theory for instance variables in an inheritance structure, but as others have pointed out, as this seems to be about an ID field, it's still better to make it private and maybe also final. Then write a public getter method, except if no one should be able to get the ID except from the subclasses, then make it protected.

Upvotes: 0

CreativeManix
CreativeManix

Reputation: 2170

Use protected, So that all the inherited classes can access it.

If the base class constrctor accepts value for entity id, like follows

class Entity
{
  protected int EntityId;
  public Entity(int _entityId)
    {
       EntityId=_entityId;
    }

}

Then you can use "super" function to call base class constructor from derived class constructor

class ExtendedEntity extends Entity
{

  public ExtendedEntity (int _entityId)
    {
       super(_entityId); // calling base class constructor
    }

}

Upvotes: 0

Hussein Zawawi
Hussein Zawawi

Reputation: 2937

You should have entityId as part of entity base type as protected.

Upvotes: 1

Andrew G
Andrew G

Reputation: 2615

First off, you can rename entityId as id as it is obviously the id of the entity. It is a member of Entity.

I will assume that id cannot be changed and as such it should be private, set only once and only in the constructor. The class should have a public getId() method. This way other objects can access it in addition to subclasses.

With this implementation id can't be changed accidentally by subclasses.

Upvotes: 2

TheLostMind
TheLostMind

Reputation: 36304

From a design perspective, entityID should be part of the entity. So, place it in Entity class and make it protected so that its subclasses can access it.

Upvotes: 0

Related Questions