package problem
we make a class in java in one package. example
package p1;
class protection{
int a=2;
}
and then we save this. with开发者_运维技巧 protection.java and compile this class compiles easily - no error. Then we make other class in same packge derived and extend the class protection and save this with derived.java example
package p1;
class derived extends protection{
derived(){
System.out.println(a);
}
}
but when we compile this comes an error:
"class protectiion not found"
and not acccess the integer a; but in book write "sub class in same package access the member". but this is not compile and not access the class. how we do this. Please if you know then help me..
If you are already in p1 when compiling, the other class is searched for in p1/p1/, because the first p1 is the current dir.
javac -cp .. derived.java
should do it.
A better, more clear idea is, to
cd ..
javac p1/derived.java
Then 'derived' and 'protected' live close and peaceful together.
精彩评论