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