Protected access & Inheritance

B

bagarow

I am trying the following code and running into an error that I am not
able to diagnose.

In a subdirectory programming/test, I have a file Door2.java with the
following code:

package test;

public class Door2 {
private boolean state = false;
protected void open () {
state = true;
}
public void close () {
state = false;
}
protected void changeState(boolean state) {
this.state = state;
}

public String toString() {
return "Door is " + state;
}
}

In the base directory programming, i have a file Car2.java with the
following code:

import test.*;

public class Car2 extends Door2 {
Door2 d = new Door2();
public static void main(String[] args) {
Car2 c = new Car2();
//System.out.println(c.d.state);
System.out.println(c.d);
c.d.open();
System.out.println(c.d);
c.d.changeState(false);
System.out.println(c.d);
}

}

I compiled Programing\test\Door2.java from the Programming directory
using

javac -d . test\Door2.java

and it compiled fine.

Then I tried to compile Programming\Car2.java from the Progamming
directory using

javac Car2.java

and I got the following 2 errors:

Car2.java:9: open() has protected access in test.Door2
c.d.open();
^
Car2.java:11: changeState() has protected access in test.Door2
c.d.changeState();
2 errors:

I would think that given Car2 is inheriting from Door2, that Car2 would
have access to the open() and changeState() methods in Door2.

I would appreciate your help in this.

Thanks
Bob
 
C

Chris Smith

javac Car2.java

and I got the following 2 errors:

Car2.java:9: open() has protected access in test.Door2
c.d.open();
^
Car2.java:11: changeState() has protected access in test.Door2
c.d.changeState();
2 errors:

I would think that given Car2 is inheriting from Door2, that Car2 would
have access to the open() and changeState() methods in Door2.

Protected access is odd. A protected field can only be accessed from a
subclass if the reference used to access the protected field has that
subclass type. Thus, you can't access protected fields of the Door2
object unless it is also a Car2 object. The intent is to restrict
protected field access to those situations in which you really are
working on the implementation of the object in question. Otherwise, you
could access the protected fields of any old object that comes your way
simply by declaring some do-nothing subclass of that other object's
class.
 
F

Fred Kleinschmidt

I am trying the following code and running into an error that I am not
able to diagnose.

In a subdirectory programming/test, I have a file Door2.java with the
following code:

package test;

public class Door2 {
private boolean state = false;
protected void open () {
state = true;
}
public void close () {
state = false;
}
protected void changeState(boolean state) {
this.state = state;
}

public String toString() {
return "Door is " + state;
}
}

In the base directory programming, i have a file Car2.java with the
following code:

import test.*;

public class Car2 extends Door2 {
Door2 d = new Door2();
public static void main(String[] args) {
Car2 c = new Car2();
//System.out.println(c.d.state);
System.out.println(c.d);
c.d.open();
System.out.println(c.d);
c.d.changeState(false);
System.out.println(c.d);
}

}

I compiled Programing\test\Door2.java from the Programming directory
using

javac -d . test\Door2.java

and it compiled fine.

Then I tried to compile Programming\Car2.java from the Progamming
directory using

javac Car2.java

and I got the following 2 errors:

Car2.java:9: open() has protected access in test.Door2
c.d.open();
^
Car2.java:11: changeState() has protected access in test.Door2
c.d.changeState();
2 errors:

I would think that given Car2 is inheriting from Door2, that Car2 would
have access to the open() and changeState() methods in Door2.

Why do you create a variable or type Door2 ("d") in Car2?
A Car2 *IS* a Door2.

You cannot access d.open() - it is protected.
You can, however, access c.open() since c is an instance of a subclass of
Door2,
and therefore can access its protected methods.
 

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
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top