Reputation: 1561
C:\Users\jaina_000\Desktop\learn_java\p1>javac Testp1.java Testp1.java:6: error: cannot find symbol Protection ob = new Protection(); ^ symbol: class Protection location: class Testp1 Testp1.java:6: error: cannot find symbol Protection ob = new Protection(); ^ symbol: class Protection location: class Testp1 Testp1.java:7: error: cannot find symbol Derived ob1 = new Derived(); ^ symbol: class Derived location: class Testp1 Testp1.java:7: error: cannot find symbol Derived ob1 = new Derived(); ^ symbol: class Derived location: class Testp1 Testp1.java:8: error: cannot find symbol SamePackage ob2 = new SamePackage(); ^ symbol: class SamePackage location: class Testp1 Testp1.java:8: error: cannot find symbol SamePackage ob2 = new SamePackage(); ^ symbol: class SamePackage location: class Testp1 6 errors
package p1;
public class Testp1
{
public static void main(String a[])
{
Protection ob = new Protection();
Derived ob1 = new Derived();
SamePackage ob2 = new SamePackage();
}
}
package p1;
public class Protection
{
int n = 1;
private int n_pri = 2;
protected int n_pro = 3;
public int n_pub = 4;
public Protection()
{
System.out.println("Inside 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);
}
}
package p1;
class Derived extends Protection
{
Derived()
{
System.out.println("Inside Derived 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);
}
}
package p1;
class SamePackage{
SamePackage(){
Protection p = new Protection();
System.out.println("Inside SamePackage constructor.");
System.out.println(" n = "+p.n);
// System.out.println("n_pri = "+p.n_pri);
System.out.println("n_pro = "+p.n_pro);
System.out.println("n_pub = "+p.n_pub);
}
}
Upvotes: 0
Views: 818
Reputation: 9872
when you have package you can't simply compile or run like you do in classes which doesn't has package .
when you compiled you need to navigate to outside of your package folder and then compile using command javac [package]/[class]
.so in your case it should be
javac p1/Testp1.java
and when run use this command java[package.class]
..so in your case it should be
java p1.Testp1
make sure you are not inside package [p1] you should outside of package [compile after navigate to folder learn_java through cmd].
finally this is how your cmd looks like
C:\Users\jaina_000\Desktop\learn_java>javac p1/Testp1.java C:\Users\jaina_000\Desktop\learn_java>java p1.Testp1 Inside base constructor. n = 1 n_pri = 2 n_pro = 3 n_pub = 4 Inside base constructor. n = 1 n_pri = 2 n_pro = 3 n_pub = 4 Inside Derived constructor. n = 1 n_pro = 3 n_pub = 4 Inside base constructor. n = 1 n_pri = 2 n_pro = 3 n_pub = 4 Inside SamePackage constructor. n = 1 n_pro = 3 n_pub = 4 C:\Users\jaina_000\Desktop\learn_java>
Upvotes: 1