Search code examples
javaaccess-modifiers

How to access sub Class in different Package?


In Package 1 i have Mainclass and SubClass. if in the package 2 How to access to Subclass?

If we want to access the subclass in same package but different file Java we can use such as:

  package Modifier;

        public class Other {
        public static void main(String[] args) {
            Public ut =  new Public();
     // Access the subclass of a class in the same package different file
            Public.subPublic sub_ut = ut.new subPublic();
        sub_ut();
        }
    }     

But how to access the subclass of a Class in the different Package? Thanks...


Solution

  • I think it's doesn't make sense, but if you want to do this . check your access modifiers ,you must to use public :

    public class Public{
    public class subPublic{ //modify  access modifiers
         }
    }