Default Access Modifier in Java:

Package:

  • Packages are folder structure.
  • Packages as the name suggests, is for packing related classes into one group.
  • we write at the beginning of any class file to indicate where exactly that class has to be created.
  • It helps organize our classes into a folder structure and make it easy to locate and use them. More importantly, it helps improve re-usability.
  • Each package in Java has its unique name.
  • There are two types of packages in java.
    • Built-in package: Already defined packages(Will learn later).
    • User defined package: The package which we create is called user-defined package.
  • Syntax: package package_name;

Import:

  • import keyword is used to import a package, class or interface.
  • It is used to make other classes and interfaces available and accessible to the current source code.
  • In simple words, if a class wants to use another class in the same package, the package name does not need to be used. Classes in the same package find each other.
  • Classes from different packages cannot find each other. Here, we can used import keyword for importing a class.
  • The import statements must be placed on top of the source file, only after the package statement.

Default Access Modifier: (No keyword required)

  • The access level of a default modifier is only within the package.
  • It cannot be accessed from outside the package.
  • If you do not specify any access level, it will be the default.
  • It provides more accessibility than private. But, it is more restrictive than protected, and public.

Example 1:

  • We have created two classes in same package.
    • tamilnadu.madurai –> package name
      • Company –> class name
      • Employee_In_Madurai –> class name
  • In Company Class, Variables and methods are declared without any modifiers, So they are default.
  • We can access default modifier from same package.
  • So, we are accessing Company Class variable & method from Employee_In_Madurai Class.

Company Class:

package tamilnadu.madurai;
public class Company
{

int travel = 100;
	
public static void main(String[] args)
{
System.out.println("Company in Madurai");
}
	
void provideSnacks()
{
System.out.println("Take snacks");
}

}

Employee_In_Madurai Class:

package tamilnadu.madurai;
public class Employee_In_Madurai
{

public static void main(String[] args)
{
System.out.println("Employee_In_Madurai");
Company com = new Company();
com.provideSnacks();
System.out.println(com.travel);
}

}

Output:

Example 2:

  • We have created two classes in different packages.
    • tamilnadu.madurai –> package name
      • Company –> class name
    • tamilnadu.chennai –> package name
      • Employee_In_Chennai –>class name
  • In Employee_In_Chennai Class, we imported the Company class.
  • In Company Class, Variables and methods are declared without any modifiers, So they are default.
  • Default modifiers cannot be accessed from outside the package.
  • So, we cannot accessed Company Class variable & method from Employee_In_Chennai Class.

Company Class: Refer Example 1

Employee_In_Chennai Class:

package tamilnadu.chennai;
import tamilnadu.madurai.Company;
public class Employee_In_Chennai
{

public static void main(String[] args)
{
System.out.println("Employee_In_Chennai");
Company com = new Company();
com.provideSnacks();
System.out.println(com.travel);
}

}

Output:

Solution:

  • In Company Class, If we declare the variable & method as public, we will access it from outside the package.

Leave a comment

Design a site like this with WordPress.com
Get started