I am trying to to run the file Demo.java which is calling Protection class within the same package but it is giving error This is the main class.
package p1;
// Instantiate the various classes in p1.
class Demo {
public static void main(String args[]) {
Protection ob1 = new Protection();
//Derived ob2 = new Derived();
//SamePackage ob3 = new SamePackage();
}
}
And this is the class that I want to use in the main class.
package p1;
public class Protection {
public int n = 1;
private int n_pri = 2;
protected int n_pro = 3;
public int n_pub = 4;
public Protection() {
System.out.println("base constructor");
System.out.println("n = " + n);
System.out.println("n_pri = " + n_pri);
System.out.println("n_pro = " + n_pro);
System.out.println("n_pub = " + n_pub);
}
}
It is giving this error:
$ javac Demo.java
Demo.java:6: error: cannot find symbol
Protection ob1 = new Protection();
^
symbol: class Protection
location: class Demo
Demo.java:6: error: cannot find symbol
Protection ob1 = new Protection();
^
symbol: class Protection
location: class Demo
2 errors
error: compilation failed
You should use
javac
, notjava
onlyWhen you use the command
java
, you can execute a file, but only the classes in that file. Here you have several files, so you should compile them in order to use them.Do the following:
This worked and resulted in the following: