简体   繁体   中英

Java Package and Access Modifier

I created a Directory p1 under which I wrote a Demo.java program :-

package p1;

public class Demo {

     public static void main(String[] args){
         Protection ob1 = new Protection();
         Derived ob2 = new Derived();
         SamePackage ob3 = new SamePackage();
    }
}

Under the same Directory I wrote Program for Protection.java, Derived.java, SamePackage.java as follows :-

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("Base Constructor");
        System.out.println("n = " + n);
        System.out.println("n_pro = " + n_pro);
        System.out.println("n_pub = " + n_pub);
    }
}  

and

package p1;

class Derived extends Protection {

    Derived(){
        System.out.println("derived Constructor.");
        System.out.println("n = " + n);

        System.out.println("n_pro = " + n_pro);
        System.out.println("n_pub = " +n_pub);
    }
}

and

package p1;

class SamePackage {

    SamePackage(){
        Protection p = new Protection();
        System.out.println("Same Package Constructor");
        System.out.println("n = " + p.n);
        System.out.println("n_pro = " + p.n_pro);
        System.out.println("n_pub = " + p.n_pub);
    }
}  

But when I am doing javac Demo.java in p1 folder I am getting an error that it can't find Protection, Derived and SamePackage symbols. What could be wrong here where I am mistaken? Any lead will be thankfully appreciated.

Your problem has nothing to do with access specifiers, its related to missing required classes during compilation.

You need to compile Protection & Derived classes before compiling Demo class. As your code in Demo class is using Protection and Derived classes so those classes should be compiled otherwise compiler will not be able to find these classes and will generate error during Demo class compilation.

As per your classes the order of compilation should be:

  1. Protection (independent class)
  2. Derived (depends on Protection)
  3. Demo (depends on Protection and Derived)

Compile Protection and Derived first. Then compile Demo class.

Rule:

Compile the composing classes BEFORE compiling the composed classes.

Example:

package com.vivek.one;

class A{



}

package com.vivek.two;

class B{



}


package com.vivek.three;

import  com.vivek.one.A;
import  com.vivek.two.B;

class C{

  A a = new A();
  B b = new B();

}

Compiling:

javac A.java
javac B.java
javac C.java

Running:

java C

Compile in this order by exucuting This command >javac -d . FileName.java

 Protection-->>SamePackage-->>Derived-->>Demo

After all file compilation use >java p1.Demo.java

Sure it will work for you

All the answers here were suggesting that I need to compile in certain order to get this problem fixed.

But what worked for me was when I ran

    >javac Demo.java Protection.java SamePackage.java Derived.java

ie I compiled all the various dependent source codes simultaneously.

I know now what works but now my curiosity forces me to know why this works? I am putting this as part of question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM