Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

/*creating first package will be stored as directory named as same of package & class will be saved in it as .

java */ package p1; public class classA { public void displayA() { System.out.println("Class A"); } } class classD //we are hiding this class { public void displayD() { System.out.println("Class D"); } }//compile this class in same directory as of package //importing first package import p1.classA; class packagetest { public static void main(String args[]) { classA obj=new classA(); obj.displayA(); } }//END /*creating second package will be stored as directory named as same of package & class will be saved in it as .java*/ package p2; public class classB { protected int m=10; public void displayB() { System.out.println("Class B"); System.out.println("m = "+m); } }//compile this class in same directory as of package //importing both packages import p1.classA; import p2.*; class packagetest1 { public static void main(String args[]) { classA obj1= new classA(); classB obj2=new classB(); obj1.displayA(); obj2.displayB(); } }//END package p1.p3;//CREATING HIERARCHY OF PACKAGES, the package p3 will be stored in side the p1\p2 directory public class classC

{ public void displayC() { System.out.println("Class C"); } }//compile this class in same directory as of package //importing hierarchy of packages import p1.p3.classC;//calling hierarchy of packages import p1.*;//calling all packages inside the package p1 class packagetest2 { public static void main(String args[]) { classC obj1= new classC(); classA obj2=new classA(); obj1.displayC(); obj2.displayA(); } }//END

You might also like