1

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?

MarioDS
  • 12,895
  • 15
  • 65
  • 121
user1479802
  • 201
  • 3
  • 12
  • 1
    Put it on Entity, set it in the constructor, keep the field private and have protected getter method (also on Entity). This restricts access from outside of the inheritance/package and it keeps the field immutable, which is probably what you're after for ID fields. – Dan Temple Jul 18 '14 at 10:47

6 Answers6

2

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.

Andrew G
  • 2,596
  • 2
  • 19
  • 26
  • This makes testing quite hard though. So long as that recognised up front, it's not an issue (since test classes can use reflection / PropertyAccessorFactory to get around that), but it is reasonable to use package access on an id field to increase testability. – Paul Hicks Jul 18 '14 at 11:16
  • That's generally true, but the id shouldn't change and therefore it's reasonable to keep it private with a public accessor. – Andrew G Aug 11 '14 at 12:25
1

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

Hussein Zawawi
  • 2,907
  • 2
  • 26
  • 44
0

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.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

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.

MarioDS
  • 12,895
  • 15
  • 65
  • 121
  • 1
    Protected is the same as default/package access with the ability for subclasses to access too (even if they are in a different package) – Dan Temple Jul 18 '14 at 10:50
0

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
    }

}
CreativeManix
  • 2,162
  • 1
  • 17
  • 29
  • In case we do not have default constructor for Entity, then what is the difference of having it as private/protected. – user1479802 Jul 18 '14 at 10:59
0

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);
    }
}
P_W999
  • 1,017
  • 12
  • 26