1

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.

1
  • @KamleshArya ,All classes are in same oackage so no need to import them.. Nov 18, 2013 at 6:19

5 Answers 5

1

Compile Protection and Derived first. Then compile Demo class.

1

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
4
  • Thanks for clearly elucidating the rule. But when we dont package the classes then the compiler automatically finds the relevant classes and compiles them isn't it so?
    – John Doe
    Nov 18, 2013 at 6:04
  • @user1590011 no compiler doesn't compile automatically...you have to compile mammually.. Nov 18, 2013 at 6:07
  • @KuldeepChoudhary try compiling the above classes without any package decalration at the top i.e all classes are in default package then you can directly compile Demo.java and run it also.
    – John Doe
    Nov 18, 2013 at 6:49
  • @user1590011 ,Sorry yar,i didn't read your upper comment carefully ,i answered for this your question context....Compiling is compelsery only when we use packaging....no need for default package.... Nov 18, 2013 at 6:54
1

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)
7
  • @user1590011 it has nothing to do with access specifier. This rule is applicable for any class referring any other class. Nov 18, 2013 at 5:46
  • 1
    @R.J i don't think imports are required if all the classes are in same package. Nov 18, 2013 at 5:50
  • here when I trying to compile the Derived class I am getting that same error its saying can't find the symbol Protection and other errors like can't find symbol n and n_pro, n_pub .
    – John Doe
    Nov 18, 2013 at 5:54
  • @user1590011 I have mentioned the order of compilation in my edited answer. Nov 18, 2013 at 5:56
  • I am compiling Derived after compiling Protection but even then I am getting the error i.e I am first compiling the Protection.java file and then Derived.java . Protection.java is compiling without any glitch
    – John Doe
    Nov 18, 2013 at 5:58
0

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

0
0

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

i.e 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.