PRIVATE ACCESS MODIFIER

  • In Private Access Modifier, Visible inside the same Class Only.
  • More Restrictive Access Modifier.

The scope of private modifier is limited to the class only.

  1. Private Data members and methods are only accessible within the class
  2. Class and Interface cannot be declared as private
  3. If a class has private constructor then you cannot create the object of that class from outside of the class.

NOTE:

1,Other Classes Cannot be Accessed

2,Can be present before Variable.

WHAT IS PACKAGE IN JAVA?

Package in java is a collection of classes, sub-packages and interfaces.

It helps organize your classes into a folder structure and make it easy to locate and use them.

More importantly, it helps improve code reusability.

NOTE:

In Package, to Compile like this,

javac -d . Travel1.java

to run,

java Trip.Travel1

SAMPLE PROGRAM:

TRAVEL1 CLASS:

package Trip;
public class Travel1{
String name=”RAJA”;
int age=20;
private String mail_id=”R@gmail.com”;

public static void main(String[]args)
{
Travel1 Traveller=new Travel1();
System.out.println(Traveller.name);
Traveller.Route();
Traveller.Reason();
Traveller.Mobileno();
System.out.println(Traveller.age);
System.out.println(Traveller.mail_id);

}
public void Route()
{
System.out.println(“Tanjore to Hosur”);
}
private void Reason()
{
System.out.println(“Emergency Purpose”);
}
private void Mobileno()
{
System.out.println(“88888”);
}

}

MEDICAL_EMERGENCY CLASS:

package Trip;
public class Medical_Emergency{
String name =”Apollo”;
String Ward = “Emergency”;
String Dr_Name=”Rajesh”;
int Advance=50000;

public static void main (String[]args)
{
Medical_Emergency Hospital=new Medical_Emergency();
System.out.println(Hospital.name);
System.out.println(Hospital.Ward);
Hospital.Stay();
System.out.println(Hospital.Dr_Name);
System.out.println(Hospital.Advance);
Hospital.Bal_Amount(Hospital.Advance);

Travel1 Receptionist = new Travel1();
System.out.println(Receptionist.name);
System.out.println(Receptionist.age);
//System.out.println(Receptionist.mail_id);
//Receptionist.Reason();
//Receptionist.Mobileno();

}
public void Stay()
{
System.out.println(“1 Week”);

}

public void Bal_Amount(int A)
{

System.out.println(A+30000);

}

}

OUTPUT:

Leave a comment

Design a site like this with WordPress.com
Get started