I have two packages p1 and p2 The code for p1 is given below
The code for package p2 is given below
When i try to compile child_class.java it says that the variable x cannot be found and the class inherit cannot be found. I have never used more than a single source file for compilation till now(Just got the certification book and have started reading it). I searched online and got results which had to do something to do with classpath. OR is it my mistake? Help...
First of all, the code you posted shows child_class declared as being in package p1, not p2. Also, fully qualified package and class names are expected to match the file system structure and locations of source files in folders.
That is, if you have p1 and p2 which you want to have the classes inherit and child_class, respectively, the folder structure and sources files should look something like this:
where .. is the path of the folder from which you compile your code.
Classes in the same package can reference each other without an import. You only need an import if the class being referenced is in a different package. Finally, as it is now, child_class cannot reference x because child_class does not extend the inherit class.