Protected or private - No difference in this context?

?

-

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?
 
S

Stefan Schulz

public class SomeClass {

protected ObjectX x = ...;
}

public class SubSomeClass extends SomeClass {

public SubSomeClass() {
x = // do assignment or invoke methods

Handles x
public class SubSomeClass extends SomeClass {

public SubSomeClass() {
ObjectX x = getObjectX();
x = // do assignment or invoke methods

Handles local copy of x
}
}

If I understand correctly, both are manipulating the same objects.. So
in that case, I should go with protected?

You do not have pointers in java. Never had, and hopefully never will.
You get a copy of the reference, and what you do with your copy is
entirely up to you.
 
B

Bjorn Abelli

- said:
I am deciding whether to make some of my
variables protected or private.

There's a slight difference on what you can do...
public class SomeClass {
protected ObjectX x = ...;
}

public class SubSomeClass extends SomeClass {
public SubSomeClass() {
x = // do assignment or invoke methods


When it's "protected" you can assign a new object to the variable x, and you
can invoke methods on the object assigned to x, but...
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

....in this scenario, you can invoke methods on x, but you can't assign a new
object to the instance variable. If you assign anything to the x you get
here, it's just locally to the local x inside the method, constructor, or
whatever.
If I understand correctly, both are manipulating the same objects..

Assignments are not manipulating any objects, just the values of the
variables, which in this case means replacing the reference to one object
with a reference to another.
So in that case, I should go with protected?

If you want to make it possible for the subclasses to assign a reference to
new object to x, you can either make x protected, or provide a mutator for
it, just as you in the latter example provided an accessor:


public class SomeClass {

private ObjectX x = ...;

public ObjectX getObjectX() {
return x;
}

protected void setObjectX(ObjectX x) {
this.x = x;
}
}

// Bjorn A
 
S

Stefan Schulz

Sounds naive. ;-)

Well, i do know the dirty tricks around no pointers, but honestly,
castrated pointers (a.k.a References) are much more well-behaved, and
won't usually make nearly as much of a mess. ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top