Can parent package classes be accessed from child package classes in Java?

Can I access from a Java sub package class? Please explain me any one?

Example package a.a1 A2 contains class sub package a contains class sup

Access sup from sub anyway?

Please explain

If I try to import it, it will not work properly. Example: Package1 contains Package1 before the program directory structure Java – > package2 – > Package3 contains pcheck java

//package1.java
package package1;
public class package1{
    public static void main(String[] args) {

    }
}
class phelo{
    phelo(){
        int a;
        System.out.println("hai fun freom package 1");
    }
}

//PCheck.java;
package package1.package2.package3;
import package1.*;   //to import package1.java
public class PCheck {
    public static void main(String[] args) {
        phelo obj=new phelo();
    }
}
class helo{
    helo(){
        int a;
        System.out.println("hai fun from package 3");
    }
}

Output: compile time error: package Package1 package2. Package3 does not exist;

For importing classes from different directories, we use the import statement, but here we need to import classes from subpackage I access the parent package and try to import it. Pls explain an example

Solution

Java does not recognize the concept of a sub - package As far as Java is concerned, packages a and A.B and A.B.C are irrelevant They are just names

Therefore, if you want to access a.b.someclass from a.b.c.someotherclass, you must use the fully qualified class name or add an import to someeotherclass

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>