Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

SUBJECT: Java TOPIC: Access control and packages

WRITTEN BY: SRINIVAS YADLAPATY, MTech,(PHD) in Computer


Science Engineering
---------------------------------------------------------------------------------
Access control:
Users to have control over the accessibility of data there are 4 access specifiers
available in java. They are private, default(not using any of the access specifier),
protected and public. An access specifier represents how to access or read the
members of a class or the class itself.

private No specifier protected Public

Same
class(A) and
Yes Yes Yes Yes
same
package(p1)
Different
class(B)(exte
nds A), but No Yes Yes Yes
same
package(p1)
Different
class(C) (non
extending No Yes Yes Yes
A), but same
package(p1)
Different
class(D)(ext
ends A, but No No Yes Yes
different
package(p2)
Different
class(E)(non
extending
No No No Yes
A), but
different
package(p2)
The least restricted access specifier is public. The most restricted access specifier
is private. No specifier acts just like public with in the same package context.
Packages:
A package represents a sub directory to store classes and interfaces.
Advantages of packages:
1. Classes and interfaces are hidden in packages. This prevents accidental
deletion.
2. The classes of one package are isolated from the classes of another
package. It is possible to use same names for classes in 2 different
packages.
Example:
Java.util.Date and java.sql.Date
Date class is present in both util and sql packages.
3. Packages provide reusability and it is the main advantage of package
concept. All the packages are in a library.
4. We can also develop our own packages and extend already available
packages.
5. A package may consists of sub packages.
User defined packages or custom defined packages:
Program:
// Creating user defined package.
package p1;
public class A
{
public static int a=4;
public static void main(String args[])
{
System.out.println("a in p1:"+a);
}
}
Compile: javac -d . A.java
Run: java p1/A
Output:
a in p1:4
or
Compile: javac -d . A.java
Run: java p1.A
Output:
a in p1:4
Compilation: javac -d . A.java
‘. ‘represents current directory. After compilation in the current directory we
can notice a sub directory created with name ‘p1’. If we double click the folder
p1 then we can find A.class file in it(i.e., a compiled .class file is spontaneously
generated soon after typing javac -d . A.java . But note that before ‘.’ operator
we have to provide space and also after ‘.’ operator also we have to provide
space.
Execution: java p1.A or java p1/A
P1.A or p1/A is called as fully qualified path or fully qualified address.
java p1\A is also correct but, works only in UNIX operating system.
Program:
// An interface in package.
package p1;
public interface A1
{
void greet();
}
Save as: A1.java
Compile: javac –d . A1.java
// Importing p1.A1 into B1 class.
import p1.A1;
class B1
{
public void greet()
{
System.out.println("Hi");
}
public static void main(String args[])
{
B1 b=new B1();
b.greet();
}
}
Compile: javac -d . A1.java
Compile: javac B1.java
Run: java B1
Output:
Hi
Program:
// Program on package inside another package.
package pack3.pack4;
public class AdditionDemo
{
public static void main(String args[])
{
int a=5,b=4;
System.out.println("Sum="+(a+b));
}
}
Compile: javac -d . AdditionDemo.java
Execute: java pack3.pack4.AdditionDemo
or
java pack3.pack4/AdditionDemo
or
java pack3/pack4.AdditionDemo
Output:
Sum=9
or
Compile: javac -d . AdditionDemo.java
Run: java pack3/pack4/AdditionDemo
Output:
Sum=9
Program:
// Program on access control in packages.
package pack1;
public class Display1
{
private int s_pri=1;
int s=2;
protected int s_pro=3;
public int s_pub=4;
public Display1()
{
System.out.println("We are in pack1.Display1,which is a super class.");
System.out.println("Private s_pri:"+s_pri);
System.out.println("No modifier s:"+s);
System.out.println("Protected s_pro:"+s_pro);
System.out.println("Public s_pub:"+s_pub);
}
}
// Program on access control in packages.
package pack1;
class Display2 extends Display1
{
Display2()
{
System.out.println("We are in pack1.Display2,which is a sub class of
pack1.Display1.");
// This will not work: System.out.println("Private s_pri:"+s_pri);
System.out.println("No modifier s:"+s);
System.out.println("Protected s_pro:"+s_pro);
System.out.println("Public s_pub:"+s_pub);
}
}
// Program on access control in packages.
package pack1;
public class Display3
{
public Display3()
{
System.out.println("We are in pack1.Display3,which is not a sub class.");
Display1 d=new Display1();
// This will not work: System.out.println("Private s_pri:"+d.s_pri);
System.out.println("No modifier s:"+d.s);
System.out.println("Protected s_pro:"+d.s_pro);
System.out.println("Public s_pub:"+d.s_pub);
}
}
// Program on access control in packages.
package pack2;
public class Display4 extends pack1.Display1
{
public Display4()
{
System.out.println("We are in pack2.Display4, which is a sub class of
pack1.Display1.");
// This will not work: System.out.println("Private s_pri:"+s_pri);
// This will not work: System.out.println("No modifier s:"+s);
System.out.println("Protected s_pro:"+s_pro);
System.out.println("Public s_pub:"+s_pub);
}
}
// Program on access control in packages.
package pack2;
public class Display5
{
public Display5()
{
pack1.Display1 d=new pack1.Display1();
System.out.println("We are in pack2.Display5, which is not a sub class.");
// This will not work: System.out.println("Private s_pri:"+d.s_pri);
// This will not work: System.out.println("No modifier s:"+d.s);
// This will not work: System.out.println("Protected s_pro:"+d.s_pro);
System.out.println("Public s_pub:"+d.s_pub);
}
}
// Program on access control in packages.
package pack1;
public class DisplayDemo1
{
public static void main(String args[])
{
Display1 d1=new Display1();
Display2 d2=new Display2();
Display3 d3=new Display3();
}
}
Compile: javac -d . Display1.java
Compile: javac -d . Display2.java
Compile: javac -d . Display3.java
Compile: javac -d . DisplayDemo1.java
Run: java pack1.DisplayDemo1
Output:
We are in pack1.Display1,which is a super class.
Private s_pri:1
No modifier s:2
Protected s_pro:3
Public s_pub:4
We are in pack1.Display1,which is a super class.
Private s_pri:1
No modifier s:2
Protected s_pro:3
Public s_pub:4
We are in pack1.Display2,which is a sub class of pack1.Display1.
No modifier s:2
Protected s_pro:3
Public s_pub:4
We are in pack1.Display3,which is not a sub class.
We are in pack1.Display1,which is a super class.
Private s_pri:1
No modifier s:2
Protected s_pro:3
Public s_pub:4
No modifier s:2
Protected s_pro:3
Public s_pub:4
// Program on access control in packages.
package pack2;
public class DisplayDemo2
{
public static void main(String args[])
{
Display4 d4=new Display4();
Display5 d5=new Display5();
}
}
Compile: javac -d . Display4.java
Compile: javac -d . Display5.java
Compile: javac -d . DisplayDemo2.java
Execute: java pack2.DisplayDemo2
We are in pack1.Display1,which is a super class.
Private s_pri:1
No modifier s:2
Protected s_pro:3
Public s_pub:4
We are in pack2.Display4, which is a sub class of pack1.Display1.
Protected s_pro:3
Public s_pub:4
We are in pack1.Display1,which is a super class.
Private s_pri:1
No modifier s:2
Protected s_pro:3
Public s_pub:4
We are in pack2.Display5, which is not a sub class.
Public s_pub:4
Interview Questions:
1. Do I need to import java.lang package any time? Why?
or
2. Which package is always imported by default?
A. No. It is by default loaded internally by the JVM. The java.lang package is
always imported by default.
3. Can I import same package/class twice? Will the JVM load the package
twice at runtime?
A. One can import the same package or same class multiple times. Neither
compiler nor JVM complains anything about it. And the JVM will internally
load the class only once no matter how many times you import the same
class.
4. Does importing a package imports the sub packages as well? E.g. Does
importing com.bob.* also import com.bob.code.*?
A. No you will have to import the sub packages explicitly. Importing com.bob.*
will import classes in the package bob only. It will not import any class in
any of its sub package’s.
5. What is a Java package and how is it used?
or
6. Explain the usage of Java packages.
A. A Java package is a naming context for classes and interfaces. A package is
used to create a separate name space for groups of classes and interfaces.
Packages are also used to organize related classes and interfaces into a
single API unit and to control accessibility to these classes and interfaces.
For example: The Java API is grouped into libraries of related classes and
interfaces; these libraries are known as package.

You might also like