Java Packages

Java Packages

A package is a collection of classes and related interfaces.
Using java packages provide protection level of access.
An major role of the package is the namespaces.
Java packages are used to facilitate finding and using classes to avoid name conflicts and to control access to classes.

To use a class or call a method of a package it must be public.

Package example

Package 1

package Pack1;

public class Class1{
    String name = "C1";
    
    public void test1(){
        System.out.println("My class is: " + name);
    }    
}

Package 2

package Pack2;

import Pack1.*;

public class Class2 extends Class1{  
    
    public static void main(String args[]){ 
        Class1 c = new Class1();  
        c.test1();  
    }  
}