Sunday, February 8, 2009

Access modifiers

class Modifiers:
Once we are writing a class the access modifier describes the properties of that class .(creation of child class is allowed or not ,creation of object is allowed or not ,we can access this class from outside the package or not and so on).
Access specifiers or Access modifiers are same in case of java.java compiler shows the error related to public,static,.......so on , it use the word modifier not Specifier.
The allowed modifiers for the Top level classes are :
1.public 2.default 3. final 4.abstract 5. strictfp
If we are using other then these modifiers ,compile time error saying modifier some not allowed here.
For the inner classes the following are allowed modifiers:
1.public 2. private 3.protected 4.default 5.final 6.abstract 7.static 8.strictfp



public class:
A public class can be accessed from any where with in the package /outside the package.
package pack1;
public class B
{
System.out.println(“hai”);
}
package pack2
{
import pack1.B;
class A
{
public static void main(String arg[])
{
B b=new B(); // we can access
}

class:
If a class declared as the default ,we are allowed to access that class only with in the current package.If you are trying to access from outside package compilation fails.
package pack1;
class B
{
System.out.println(“hai”);
}
package pack2
{
import pack1.B;
class A
{
public static void main(String arg[])
{
B b=new B(); //we can't access
}
Here class B is is default ,but not public in pack1.So this class can’t be accessed from outside package.
final class:
If a class declared as the final then we are not allowed to create the child class.
Advantage:
We can implement security .
Limitation:As we are not allowed to create the child class,we are missing the key benefits of object oriented programming ,re usability and flexibility.Its not a good programming practice to use final classes in the real time unless security is required.
final method:
If a method declared as the final ,it indicates that this implementation is final implementation.
i.e we are not allowed to override this implementation the child class.
Observations:

  • If a class declared as the abstract ,we should create the child class to provide implementation for abstract methods.
  • If a class declared as the final ,you are not allowed to create the child class.Hence final and abstract combination is illegal for classes.
Example:
class Sample
{
void m1(); //error: missing method body or declaration missing.
public static void main(String arg[])
{
System.out.println(“hai”);
}
}

  • Abstract methods should be overridden in the child class to provide implementation.But final methods are not allowed to override .Hence abstract and final combination is illegal for methods also.
  • A final class never allowed to contain abstract methods .but a final methods is allowed to keep inside abstract class.i,e final method in abstract class is valid But abstract method in final class is not valid.
  • Abstract is the keyword which can be applied for classes and methods only.i.e we can’t use abstract keyword for a variable.
  • final is the keyword which can be applied for classes ,methods and variables.
Strictfp: Strict floating point
  • This keyword we can apply for classes and methods .i.e we can’t apply strictfp for the variables.
  • If a method declared as the strictfp all the floating point calculations inside that method has to follow “IEEE 754” standards so that we will get platform independent result.
  • If a class declared as the strictfp ,all concrete methods in that class has to follow IEE 754 standard for floating point calculations .
  • Abstract and strictfp combination is not-valid for methods .But it is valid combination for the classes.
Not-valid combinations:
abstract and final .
abstract and strictfp
abstract and synchronized
abstract and static
abstract and native
abstract and private.

Method level Modifiers and Variable level Modifiers:
public members:
If a member is declared as public ,we are allowed to access that member from any where with in the package or outside the package.
package pack1;
class A
{
public void m1()
{
System.out.println(“hai”):
}
}
package pack2;
import pack1.A;
class B
{
public static void main(String arg[])
{
A a=new A(); //error
a.m1();
}
}
  • The corresponding class must be visible.Before checking member visibility ,first we have to check class visibility.
  • If class A is declared as public ,then the class B compiles fine and prints “Hai”.
default member:
If a member declared as the default ,that member is visible with in the current package only.
package pack1;
public class A
{
void m1()
{
System.out.println(“hai”):
}
}
package pack2;
import pack1.A;
class B
{
public static void main(String arg[])
{
A a=new A();
a.m1(); //error
}
}
error:m1() is not public in pack1.A.
  • Hence default modifier is also known as package level modifier.
private members:
  • If a member declared as the private we can access with in that class only.i.e from outside the class,you are not allowed to access.
  • It is highly recommended to declare data members as private to achieve security.
protected member:The most misunderstood modifier in java .If a member declared as the protected with in the current package,every where we are allowed to access,but outside the package ,we can access only in the child classes.

protected= + child classes;

Example:
package pack1;
public class A
{
protected void m1()
{
System.out.println(“hai”);
}
}
case 1:
class B
{
public static void main(String arg[])
{
A a=new A();
a.m1(); // hai
}
}
Case 2:
class B extends A
{
public static void main(String arg[])
{
B b=new B();
b.m1(); // hai
}
}
case 3:
A a1=new B();
a1.m1(); //hai
case 4:
B a=new A();
a.m1(); // error
Example:
package pack2;
import pack1.A;
class B extends A
{
public static void main(String arg[])
{
A a=new A();
a.m1();
}
case 2:
B b=new B();
b.m1(); // valid
case 3:
a a1=new B();
a1.m1(); // in-valid
conclusions:
  • The protected members can be accessed with in the current package any where either by using parent class reference or by using child class reference.
  • But from outside package we can access protected member only by using child class reference .
  • If we are using parent class reference to access protected member from outside package we will get a compile time error.
Example:
case 1:
class Sample
{
public static void main(String arg[])
{
Object o=new Object();
o.finalize(); // error
System.out.println(“hai”);
}
}
error :finalize has protected access.
Case 2:
public static void main(String arg[])throws Throwable
{
Sample s=new Sample();
o.finalize(); // valid
System.out.println(“hai”);
}
case 3:
Object o=new Sample();
o.finalize(); //in valid

final variables:
final is the keyword which can be applied for methods classes and variables.
  • Instance and static variables will get default values always .There is no need to perform explicit initialization.
  • But the local variables never get any default values .Before accessing a local variable ,we should perform initialization other wise compile time error.
final instance variables :
For the final instance variables ,we should perform initialization other wise compile time error.
The final instance variable must be initialization before constructor compiles.i.e at the time of declaration or inside instance initialization block or inside constructor.
Example:
final int i;
public static void main(String arg[])
{
Sample s=new Sample()
System.out.println(s.i); // invalid
}

final-static variables:-
Final static variables must be initialized before class loaded into memory. otherwise compile time error. i.e we can perform initialization for the final static variables at one of the following places.
1. at the time of declaration
2. inside the static initialization block
final static int I;
static
{
int i=30;
}

final-local variables:-
Local variables even though we can declared as the final must be initialized before using.
class Sample
{
Public static void main(String args[])
{
final int I;
System.out.println(“hai”);
} // hai
  • For the local variables the only allowed modifier is final.
Example:
class test
{
public static void main(String arg[])
{
final int a; //instead of final we cant write int modifier
System.out.println(“hi”);
}
}

The variables which are declared as the method arguments are simply acts as local variables of that method .hence the only applicable modifier for the logical variables is final. If any formal parameter declared as the final we are not allowed change its value with in the method.
Example:
class test
{
public static void main(String arg[])
{
m1(100,200);
}
}
public static void m1(final int i, final int j)
{
i=200; //error
i=300; //error
}
}

Static Modifier:
  • The keyword static can be use for classes,methods, variables
  • We can not apply static keyword for the top level classes,but we can apply inner classes.
  • For every object a separate copy of instance variables will be created but in the case of static variables a single copy will be created at the class level and shared by all the objects of that class.
Example:
class test
{
int i=10;
static int j=20;
public static void main(string arg[])
{
Test t1=new Test();
t1.i=100;
t1.j=200;
System.out.println(t1.i+’…”+t1.j); //100,200
Test t2=new Test();
System.out.println(t2.i+’…”+t2.j); // 10,200
t2.j=2000;
System.out.println(t1.i+’…”+t1.j); //100,2000
}
}
Most of the cases the keyword static associated with final modifier for defining class level constants.
Non static variables can not be returning form static modifier/ content:
class test
{
int i=10;
public static void main(String arg[])
{
System.out.println(i);
}
}
error: Non static variable I cant be returning static content
example:
1) int i=10;
2) static int i=10;
3)public void m1()
{
System.out.println(i);
}
4) public static void m1()
{
System.out.println(i);
}
Which of the following are not allowed simultaneously in the same class
1) & 3) instance variable can't be accessed from instance area,either instance block, constructors or instance access block

1) & 2) compile time error.. non static variables are instance variables can not be referenced from static constant

2) & 3) , 2) & 4)… static variables can be accessed from any where either from instance area or static area


3)Usually static methods are utility methods and we should provide complete implementation but for abstract methods we are not allowed to provide implementation . Hence abstrace and static combination is illegal for the methods.

4) we can overload static methods.but we can not override

Synchronized Keyword:
  • Synchronized is the keyword which can be apply for method and blocks. i.e we can not apply synchronized keyword for classes and variables.
  • If a method declared as synchronized at a time only one thread is allowed to execute that method on the given object.
Advantages:
1) We can provide thread safety
2) We can avoid data inconsistency problems
Disadvantages:
1)synchronized keyword increases waiting time of threads and hence performance of the system goes down.hence unless and until there is no specific requirement do not use synchronized keyword in the coding.
Note:Synchronized is the keyword which is always talks about implementation but abstract never talks about implementation.Hence synchronized and abstract combination is illegal for the methods.

Native Modifier:
Native is the keyword which can be apply only for methods. i.e we can not apply native keyword for classes and variables.
1) A native method means. The method which is implemented in non-java like c,c++;
2) Native methods are also known as foreign methods.
Advantages of Native method:
1) As the performance of java is low for improving the performance we can depends on c or c++. This stage methods can be helped by native keywords
2) We can communicate legacy systems by using native keyword
Demo:
class NativeEx
{
static
{
System.loadLibraries(“path of native methods library”);
}
native void m1();
}
class Client
{
public static void main(String arg[])
{
Native n=new NativeEx();
n.m1();
}
}

  • For the native methods already implementation is available but abstract method means implementation is not available .hence abstract and native combination is illegal for the methods
  • Native methods already implementation is available we are not providing any implementation. hence nativemethod declaration should be ends with semicolon
  • Native and strictfp combination is illegal for methods because old languages may not fallow IEEE 754 standerd for floating point.
  • We can override a native method
  • We can overload a native method
Transient Keyword:
Transient is the keyword which can be applicable only for variables i.e., we are not allowed to use transient keyword for methods and classes.
Serialization:
The process of saving an object to a file is called serialization Strictly serialization means “The process of converting java supported format object to network supported format object (or) file supported format object is called serialization.
1)If a variable declared as a transient, at the time of serialization JVM ignores the values of that the transient variable. Instead of original values JVM saves default value.
2) Hence transient means not to serialize.
Example:- While saving account information permanently to a file we are not allowed to save passwords for security reasons such type of variables we have to declare by transient keyword
3)We can serialize only serializable objects violation leads to runtime exception saying not serializable exception
4)An object is said to be serializable if an only if the corresponding class implements serializable interface

Transient Example:
import java.io.*;
class Transient Demo implements Serializable
{
int i=10;
int j-20;
public static void main(string args[])throws exception
{
Transient demo t1=new transient demo();
System.out.println(t1.i+”----“+t1.j);
file output stream fos=new file output stream(“abc.txt”);
object output stream oos=new object output stream(fos);
oos.writeobject(t1);
//t1.i=1000
//t1.j=2000
FileInputStream fis=new FileInputStream(“abc.txt”);
ObjectInputStream fis=new ObjectInputStream(fis);
Transient Demo t2=( Transient Demo)ois.writeObject(t1);
System.out.println(“t2.i+”…”+t2.j);

1).If we are not declaring implements serializable we will get a runtime exception not serializable exception
2)if we are not keeping throws exception compile time error saying unreported exception must be called or declared to be thrown

Note:- static variables never part of object state hence we are not participating in the serialization process during a static variables as the transient is useless and there is no impact.

Volatile Keyword:
Volatile is the keyword which can be applicable only for variables i.e we can not use for classes and methods . If the value of the variable is changing frequently such tpe of variables we have to declare with the keyword volatile.
1) For the volatile variables JVM will create a separate private copy for every thread.After completing entire transaction but that thread the final value will be updated in the master copy. So that the value of the volatile variable is always stable
2) At variable level we can achieve synchronization by using volatile keyword
3) For every thread maintaining a separate copy is always difficult .hence performance of the system goes down
4) Volatile means the value keep on changing but final means the value is not allowed to change. Hence volatile and final combination is always illegal. We are not declaring a final variable as volatile.

0 comments:

Post a Comment