Home Java Introduction Decisions Loops Classes Access Inheritance Inner Classes Arrays Exceptions Strings Generics Container Input/Output MultiThreading jdk8 Versions Books

Access


Contents



We study access and java program structure in more detail in this section.

Package and Private

If a data member does not have an access specifier then it has package access. That means any file( class ) in that package can access it. In the below file the name of the class has public access and the data member "x1" has package access. The class "access1" has no package name. That means any class in this folder can access it's pacakge access data members. In order to use a class that is another package we need to import the full package path or access it directly using the package path if we don't import it.

File: access1.java
//This will import all the classes in the
//java.util package
import java.util.* ;

//This will only import the Vector class
import java.util.Vector ;

public class access1
{
    //package access
    //a class in this package can access it
    int x1 ;

    //only this class can access it
    private int x2 ;

   //No need to import String . It is in the
   //java.lang package and that gets imported
   //by default
   public static void main( String args[] )
   {
      //Vector exists in the java.util package
      //We need to import it.
      Vector<String> v1 ;

       //If we don't have an import then use
       //fully qualified name
       java.util.Vector<String> v2 ;
   } //public static void main( String args[] )


}


File: UseAccess1.java
Another file in the same package can access the data members that have package access.
public class UseAccess1
{


   //static function acts just as if we were outside the class.
   public static void main( String args[] )
   {
       access1 access1Obj = new  access1() ;
       //ok package access
       access1Obj.x1 = 10 ;

       //Error private access
       //access1Obj.x2 = 10 ;
   } //public static void main( String args[] )


}

proteced

A proteced data member of a class can be accessed by a derived class or by classes in the same package as shown by the below program.

File: access2.java

class vehicle
{

  protected float weight ; //in lbs
  private float topSpeed ; //mph
}

class train extends vehicle
{
  public int noCarriages ;

  void printWeight()
    {
      //can access protected members but not
      //private members
      System.out.println( "weight: " + weight ) ;
    }
}




public class access2
{

   public static void main( String args[] )
   {
      train  trainObject1 = new train() ;

      //It's in the same package
      trainObject1.weight = 100 ;
   } //public static void main( String args[] )


}
Now we create a package by creating a folder in our current directory and create the train class in that folder/package. We then create a file "access3.java" which accesses the "train" class.

File: access3.java

public class access3
{

   public static void main( String args[] )
   {
      transport.train  trainObject1 = new transport.train() ;

      //It's in the same package
      //Protected access and train is in another package.
      //trainObject1.weight = 100 ;
   } //public static void main( String args[] )


}
The "train.java" exists in the "transport" folder.

File: transport/train.java
package transport ;

class vehicle
{
  protected float weight ; //in lbs
  private float topSpeed ; //mph
}

public class train extends vehicle
{
  public int noCarriages ;

  void printWeight()
    {
      System.out.println( "weight: " + weight ) ;
    }
}

Exercises:
1)

File: ex1.java














1)
File: soln1.java
2)