?
-
I am deciding whether to make some of my variables protected or private.
Consider this:
E.g With protected
public class SomeClass {
protected ObjectX x = ...;
}
public class SubSomeClass extends SomeClass {
public SubSomeClass() {
x = // do assignment or invoke methods
}
}
E.g With private
public class SomeClass {
private ObjectX x = ...;
public ObjectX getObjectX() {
return x;
}
}
public class SubSomeClass extends SomeClass {
public SubSomeClass() {
ObjectX x = getObjectX();
x = // do assignment or invoke methods
}
}
If I understand correctly, both are manipulating the same objects.. So
in that case, I should go with protected?
Consider this:
E.g With protected
public class SomeClass {
protected ObjectX x = ...;
}
public class SubSomeClass extends SomeClass {
public SubSomeClass() {
x = // do assignment or invoke methods
}
}
E.g With private
public class SomeClass {
private ObjectX x = ...;
public ObjectX getObjectX() {
return x;
}
}
public class SubSomeClass extends SomeClass {
public SubSomeClass() {
ObjectX x = getObjectX();
x = // do assignment or invoke methods
}
}
If I understand correctly, both are manipulating the same objects.. So
in that case, I should go with protected?