• As you will see, Java provides many levels of protection to allow fine-grained control over the visibility of variables and methods within classes, subclasses, and packages.

 

  • Packages act as containers for classes and other subordinate packages.

 

  • Java addresses four categories of visibility for class members:

• Subclasses in the same package

• Non-subclasses in the same package

• Subclasses in different packages

• Classes that are neither in the same package nor subclasses

 

  • The three access specifiers, private, public, and protected, provide a variety of ways to produce the many levels of access required by these categories.

 

CLASS MEMBER ACCESS

 

   

PRIVATE

 

 

NO MODIFIER

 

PROTECTED

 

 

PUBLIC

 

SAME CLASS

 

 

YES

 

YES

 

YES

 

YES

 

SAME PACKAGE SUBCLASS

 

 

NO

 

YES

 

YES

 

YES

 

SAME PACKAGE NON-SUBCLASS

 

 

NO

 

YES

 

YES

 

YES

 

DIFFERENT PACKAGE SUBCLASS

 

 

NO

 

NO

 

YES

 

YES

 

DIFFERENT PACKAGE

NON-SUBCLASS

 

NO

 

NO

 

NO

 

YES

 

1] PUBLIC

- A class has only two possible access levels: default and public.

- When a class is declared as public, it is accessible by any other code.

2] DEFAULT [friendly access]

  • If a class has default access, then it can only be accessed by other code within its same package.
  • The difference between public access and friendly access is that the public modifier makes fields visible in all classes regardless of their packages, while friendly access modifier makes field’s visibility only in the same package but not in other packages.

3] PRIVATE

- Anything declared private cannot be seen outside of its class.

- Can’t be inherited by the subclasses of other packages.

4] PROTECTED

Protected modifier makes the field visible not only to all classes and subclasses in the same package as well as in other packages.

5] PRIVATE PROTECTED

This modifier makes the fields visible in all sub classes regardless of what package they are in. These fields are not accessible by other classes in the same package.

EXAMPLE

The following example shows all combinations of the access control modifiers.

 

Following is the source code for the other package, p1.

 

This is file Protection.java:

 

package p1;

public class Protection

{           int n = 1;

private int n_pri = 2;

protected int n_pro = 3;

public int n_pub = 4;

public Protection()

{           System.out.println(“base constructor”);

System.out.println(“n = ” + n);

System.out.println(“n_pri = ” + n_pri);

System.out.println(“n_pro = ” + n_pro);

System.out.println(“n_pub = ” + n_pub);

}

}

 

This is file Derived.java:

 

package p1;

class Derived extends Protection

{           Derived()

{          System.out.println(“derived constructor”);

System.out.println(“n = ” + n);

// class only

// System.out.println(“n_pri = ” + n_pri);

System.out.println(“n_pro = ” + n_pro);

System.out.println(“n_pub = ” + n_pub);

}

}

 

This is file SamePackage.java:

 

package p1;

class SamePackage

{           SamePackage()

{          Protection p = new Protection();

System.out.println(“same package constructor”);

System.out.println(“n = ” + p.n);

// class only

// System.out.println(“n_pri = ” + p.n_pri);

System.out.println(“n_pro = ” + p.n_pro);

System.out.println(“n_pub = ” + p.n_pub);

}

}

 

Following is the source code for the other package, p2.

 

This is file Protection2.java:

 

package p2;

class Protection2 extends p1.Protection

{           Protection2()

{          System.out.println(“derived other package constructor”);

// class or package only

// System.out.println(“n = ” + n);

// class only

// System.out.println(“n_pri = ” + n_pri);

System.out.println(“n_pro = ” + n_pro);

System.out.println(“n_pub = ” + n_pub);

}

}

 

This is file OtherPackage.java:

 

package p2;

class OtherPackage

{          OtherPackage()

{           p1.Protection p = new p1.Protection();

System.out.println(“other package constructor”);

// class or package only

// System.out.println(“n = ” + p.n);

// class only

// System.out.println(“n_pri = ” + p.n_pri);

// class, subclass or package only

// System.out.println(“n_pro = ” + p.n_pro);

System.out.println(“n_pub = ” + p.n_pub);

}

}

 

Here are two test files you can use. The one for package p1 is shown here:

 

// Demo package p1.

package p1;

// Instantiate the various classes in p1.

public class Demo

{           public static void main(String args[])

{           Protection ob1 = new Protection();

Derived ob2 = new Derived();

SamePackage ob3 = new SamePackage();

}

}

 

The test file for p2 is shown next:

 

// Demo package p2.

package p2;

// Instantiate the various classes in p2.

public class Demo

{          public static void main(String args[])

{           Protection2 ob1 = new Protection2();

OtherPackage ob2 = new OtherPackage();

}

}

No tags Hide

  • Package is essentially a group of classes.
  • Packages are containers for classes that are used to keep the class name space compartmentalized.
  • For example, a package allows you to create a class named List, which you can store in your own package without concern that it will collide with some other class named List stored elsewhere.

 

  • The package is both a naming and a visibility control mechanism.
  • You can define classes inside a package that are not accessible by code outside that package.
  • You can also define class members that are only exposed to other members of the same package.

 

Defining package

  • To create a package, include a package command as the first statement in a Java source file. –
  • Any classes declared within that file will belong to the specified package.
  • The package statement defines a name space in which classes are stored. If you will not give the package name then the class names are put into the default package, which has no name.

 

  • This is the general form or syntax of the package statement:
    • package packagename;
  • Here, packagename is the name of the package.

 

  • For example, the following statement creates a package called MyPackage.
    • package MyPackage;

 

  • Java uses file system directories to store packages.
  • For example, the .class files for any classes you declare to be part of MyPackage must be stored in a directory called MyPackage.Case is significant, and the directory name must match the package name exactly.

 

  • By default JAVA runtime system uses current working directory as its starting point.

 

Characteristics of packages:

 

1]        Packages are stored in hierarchical manner and are explicitly imported into new classes

definition.

2]        The package is both naming and visibility control mechanism.

3]        If a source file doesn’t contain a package statement, its classes and interfaces are placed in a default package.

4]        Package statement must appear as the first statement.

5]        Two different packages can declare classes have same, by providing this way JAVA provides way for partitioning the class name space into more manageable chunks.

6]        We can define classes inside package that are not accessible by code outside that package. We can also define class members that are only exposed to other members of the same package.

 

By organizing classes into packages following benefits can be achieved.

 

1]        The classes contained in the packages of other programs can be easily reused.

2]        In packages, classes can be unique compared with classes in other packages. i.e. two classes in two different packages can have same name.

3]        Packages can hide classes that are meant to internal use only from other programs or packages.

4]        Packages also provide a way for separating “design” and “coding”.

1st we can design classes and decide their responsibility and then we can implement the java code needed for the methods.

It is possible to change the implementation of any method without affecting the rest of the design.

Hierarchy of packages:

 

  • You can create hierarchy of packages. To do this simply separate each package name from the one above it by use of a period.

Syntax:

package pkg1[.pkg2[.pkg3]];

 

 

CLASSPATH variable

 

  • CLASSPATH is an environment variable that determines where the JDK tools such as the JAVA compiler and interpreter search for a .class file.
  • It contains an ordered sequence of directories as well as .jar and .zip files.

 

  • It can be set as:

 

set classpath=.;c:\\project1;c:\\project2

 

  • echo %classpath% displays the current settings of the classpath variable.

 

Example:

 

package MyPack;

class Balance

{

String name;

double bal;

Balance(String n, double b)

{

name=n;

bal=b;

}

void show()

{

If(bal<0)

{

System.out.println(“->  “);

System.out.println(name+”:Rs.”+bal);

}

}

}

 

class AccountBalance

{

public static void main(String args[])

{

Balance current[]=new Balance[5];

Current[0]=new Balance[“liza”,25000.02];

Current[1]=new Balance[“liza”,17000.00];

Current[2]=new Balance[“pushpendu”,12000.00];

 

for(i=0;i<3;i++)

current[i].show();

}

}

No tags Hide

  • A multithreaded program contains two or more parts that can run concurrently.
  • Each part of such a program is called a thread, and each thread defines a separate path of execution.
  • The thread is the smallest unit of dispatchable code.
  • This means that a single program can perform two or more tasks simultaneously.
  • For instance, a text editor can format text at the same time that it is printing, as long as these two actions are being performed by two separate threads.

 

  • Multitasking threads require less overhead than multitasking processes.

 

  • Processes are heavyweight tasks that require their own separate address spaces whereas threads are lightweight. They share the same address space and cooperatively share the same heavyweight process.

 

  • Interprocess communication is expensive and limited whereas Interthread communication is inexpensive.

 

  • Context switching from one process to another is also costly whereas context switching from one thread to the next is low cost.

 

  • Process-based multitasking is not under the control of Java. However, multithreaded multitasking is under control of java.

 

  • Multithreading enables you to write very efficient programs that make maximum use of the CPU, because idle time can be kept to a minimum.

 

Two types of operating system:

 

Single threaded

Multithreaded

 

  • In a traditional, single-threaded environment, your program has to wait for each of these tasks to finish before it can proceed to the next one—even though the CPU is sitting idle most of the time.
  • Multithreading lets you gain access to this idle time and put it to good use.

No tags Hide

There are several methods used by the thread.

  • Static methods
  • Instance methods

 

  • Static methods defined in the class are as follow:
    • Thread currentThread()
      • Returns a Thread object that encapsulates the thread that calls this method.

Or

  • Returns a reference to the current thread.

 

  • void sleep(long msec) throws InterruptedException
    • Causes the current thread to wait for the msec millisecond.

Or

  • Suspends execution of the thread for the specified number of milliseconds.

 

  • void sleep(long msec, int nsec) throws InterruptedException
    • Causes the current thread to wait for the msec millisecond plus nsec nanoseconds.

Or

  • Suspends execution of the thread for the specified number of milliseconds plus specified nanoseconds.

 

  • void yield()
    • Causes the current thread to yield control of the processor (CPU) to another thread.
    • Instance methods defined in the class are as follows:
      • String getName()
        • Returns the name of the thread.
  • int getPriority()
    • Returns the priority of the thread.

 

  • boolean isAlive()
    • Returns true if the specified thread has been started and has not yet died otherwise returns false.

 

  • void join() throws InteruptedException
    • causes the caller to wait until the specified thread dies.

 

  • void join(long msec) throws InteruptedException
    • Waits up to the specified number of milliseconds for the thread on which it is called to terminate.
    • If the msec is zero (0) than there is no limit for the wait time.

 

  • void join(long msec,int nsec) throws InteruptedException
    • Waits up to the specified number of milliseconds plus nanoseconds for the thread on which it is called to terminate.
    • If the msec plos nsec is zero (0) than there is no limit for the wait time.

 

  • void run()
    • Begins execution of thread.

Or

  • Comprises the body of the thread, this method is overridden by subclass.

 

  • void setPriority(int p)
    • Sets the priority of this thread to p.

 

  • void setName(String s)
    • Set the thread’s name as the specified name of s.

 

  • void start()
    • starts the execution of hte thread.

 

  • String toString()
    • returns the string equivalent of a thread.

No tags Hide

Next »

Managed by

MNP INFOTECH