Saturday 3 August 2013

20:Access Specifiers, Constructors and Methods

Learning Objectives
After completing this session, you will be able to:
‰  Apply access modifiers
Access Modifiers

You can define the scope of a variable or method or class by using the access modifiers.
There are four different types of access modifiers in Java:
‰  public (Least restrictive)
‰  protected
‰  default
‰  private (Most restrictive)
The first three access modifiers are explicitly written in the code to indicate the access type, for the
third one, which is default, no keyword is used.
public Accessibility
Public access:
‰  Specifies that class members (variables ormethods) are accessible to anyone, both
inside and outside the class and outside of the package
‰  Any object that interacts with the class can have access to the public members of the
class
Keyword: public
Example: “public” Access Modifier
public class StudentRecord {
//default access to instance variable
public int name;
//default access to method
public String getName(){
return name;
}
}
protected Accessibility
Protected access:
‰  Specifies that the class members are accessible only to methods in that class and the
subclasses of the class
‰  The subclass can be in different packages
Keyword: protected
Example: “protected” Access Modifier
public class StudentRecord {
//default access to instance variable
protected int name;
//default access to method
protected String getName(){
return name;
}
}
default Accessibility
Default access:
‰  Specifies that only classes in the same package can have access to the variables and
methods of the class
‰  No actual keyword is their for the default modifier and it is applied in the absence of an
access modifier
Example: “default” Access Modifier
public class StudentRecord {
//default access to instance variable
int name;
//default access to method
String getName(){
return name;
}
}
private Accessibility
Private accessibility:Specifies that the class members are only accessible by the class in which
they are defined
Keyword: private
Example: “private” Access Modifier
public class StudentRecord {
//default access to instance variable
private int name;
//default access to method
private String getName(){
return name;
}
}
Java Program Structure: The Access Modifiers

Coding Guidelines
The instance variables of a class should normally be declared private, and the class will just
provide accessor (getter) and mutator (setter) methods to these variables.
Try It Out
Problem Statement:
Write a program that illustrates about access modifiers.
Code:
package learn;
public class OtherClass {
void testIt() { // No modifier means method has default access
System.out.println(“OtherClass”);
}
}
------------------------------------------------------------------------
In another source code, you have the following:
package somethingElse;
import learn.OtherClass;
class AccessClass {
static public void main(String[] args) {
OtherClass o = new OtherClass();
o.testIt();
}
}
Refer Files : OtherClass.java, AccessClass.javato obtain soft copy of the program code
How It Works:
‰  In the OtherClass program, the testIt() method has default (think: package-level)
access.
‰  Notice also that class OtherClass is ina different package from the AccessClass.
‰  Now the question is, will AccessClass be able to apply the method testIt() ? Will it
cause a compiler error?
‰  No method matching testIt()is found in class learn.OtherClass. o.testIt();.
‰  From the preceding results, you can see that AccessClass cannot use the OtherClass
method testIt() because testIt() has default access, and AccessClass is not in the
same package as OtherClass. So AccessClass cannot see it, and hence the compiler
complains.
Tips and Tricks:
Provide the key tips on access modifiers in Java.
Solution:
‰  Access modifiers can be public, protected, and private.
‰  If no access modifier is specified, then the accessibility is default package visibility. All
classes in the same package can access the feature. It is called as friendly access.
But friendly is not a Java keyword. Same directory is same package in consideration
of Java.
‰  ‘private’ means only the class can access itand not even sub-classes. So, it will
cause access denial to the variable or method of the sub class.
‰  These modifiers dictate, which classes can access the features. An instance of a class
can access the private features of another instance of the same class.
‰  ‘protected’ means all classes in the samepackage (like default) and sub-classes in
any package can access the features. But a subclass in another package can access
the protected members in the super-class byonly the references of subclass or its
subclasses. A subclass in the same package does not have this restriction. This
ensures that classes from other packages are accessing only the members that are
part of their inheritance hierarchy.

No comments:

Post a Comment