Pages

Friday, 13 September 2013

Core Java - Access Modifiers (FAQ's)




1.    What access modifiers available in Java ?

Access modifiers are:

  • public 
  • private
  • protected
  • static
  • <default>
  • final
  • abstract
  • strictfp
  • synchronize
  • native
  • transient
  • volatile





2.    What modifiers are applicable to classes ?

Modifiers applicable to top level classes are:

public:  Using this modifier, class will be accessible from anywhere either within or outside the package.

default: Using this modifier, class will be accessible within current package only. Also called "package level access".

final:     Using this modifier, we can restrict the creation of child class i.e. Extend class is not allowed for any class which is defined with final modifier. Not recommended unless required extensively because we will loose the benefits of OOPS here (Inheritance is not possible for final class).

abstract: Using this modifier, we can restrict the creation of object i.e. object cannot be created for classes which are defined with abstract modifier.

  • Class should to be declare abstract if any method inside class is defined as abstract.
  • If there is no abstract method declared inside class, still class can be declared as abstract.


strictfp: If class is defined with strictfp modifier, all methods within the same class should have to follow IEEE754 standard to achieve platform independent results.

  • For class, strictfp with abstract modifier is allowed & legal to declare.

Note:
Above access modifier (public, default, final, abstract, strictfp) are applicable to both top level classes and inner classes. But their are 3 more modifiers which are applicable to inner classes (private, protected & static).






3.    What modifiers are applicable to methods ?

public (method is accessible from within and outside package, only condition is method should be accessible

default (if no modifier is declared means method is default)

abstract : If method has to be declared without implementation then method should be declared with abstract modifier.
e.g.
  • abstract method_m1();                      <== Correct  
  • abstract method_m1()  {  };                <== Illegal statement; abstract method should not contain {} braces.
  • For any method "Abstract + native"         <== Not allowed
  • For any method "Abstract + strictfp"       <== Not allowed
  • For any method "Abstract + final"          <== Not allowed
  • For any method "Abstract + synchronized"   <== Not allowed
  • For any method "Abstract + private"        <== Not allowed
  • For any method "Abstract + static"         <== Not allowed


final: If method is declared as final, then method cannot be overridden in child class.
  • Advantage of declaring method as final is, security. No one can modify the final method. 
  • Disadvantage is, we loose benefits of OOPS here, polymorphism is not allowed for final method.

strictfp: If method is decalred with stricfp modifier then all floating point calculations in the method should follow IEEE754 standard to achieve the platform independent result.
Note: Floating point arithmetics varies from platform to platform.


private: private method is visible only within the same class. private & abstract combination is illegal because private methods are not accessible outside same class & abstract methods need to be modified in child class.

protected: protected method is visible anywhere within the same package and outside package only accessible by child class reference. Outside the package we cannot access protected method using parent reference. clone() method & finalize() methods are protected methods in object class.

static: if method is declared as static then compulsary implementation has to be declared. Overloading concept, Inheritance concept includin main() method is applicable for all static methods. Overriding concept is not applicable but if static method in main class is overridden with another static method in child class, we will not get any compile time error but it is not overriding, it is called method hiding.

native: this modifier is applicable to methods only. Methods which can be implemented in other languages like (c & c++) are known as native methods or foreign methods. such methods should declared with native modifier.

  • native method implementation is already available, so no need to provide any implementation.
  • native method should end with semi colon.
  • We cannot declare native method with strictfp because we are not sure if native method is following IEEE754 standards.
  • Inheritance, overriding & overloading concepts are applicable for native method.
  • native and abstract are illegal combinations because abstract method implementation has to be declared & native method implementation is already available.
  • Advantage - high performance
  • Disadvantage - breaks platform independence nature of java. 

synchronize: If method is declared as synchronized then only one thread can perform operation on method on the given object.

  • Advantage - data consistency
  • Disadvantage - Increase waiting time for other threads in queue which reduces performance.
  • synchronized & abstract is illegal combination because synchronized require compulsory implementation.




4.    Where transient and volatile modifiers are being used ?

Transient & volatile modifiers are applicable to variables only.

transient: this modifier is used in serialization. If we want to hide some values like password or security keys etc. then during serialization, if variable is declared transient, then JVM will ignore original value and only default value will be considered.

e.g. we want to save bank account number & its password to a file. We should declare password as transient. This way JVM will save default value of password to file during serialization instead of original password. Advantage is security.

account_number   ==>  |                  |         [account num]
                 ==>  |==> Serialization |==> store in file 
account_password ==>  |                  |         [password]

In above example, if account number = 101010101  
                           password = hadoopguru   (transient)

then file will contain account number = 10101010 &
                             password = null




volatile: if variable is declared as volatile then for each thread separate copy of variables created & all intermediate enhancements perform by threads will be done in local copy of that variable.

  • Advantages - this will solve data inconsistency problem, all threads are not performing modifications on same variable instead separate copy is created for each thread.
  • Disadvantage - maintenance of separate copy of variable for each thread is complex & it will also reduce the performance.
  • Volitile modifier is outdated and not used often now a days. 





High Level view of Access modifiers


Note:  Highlighted in Green is legal declaration.





More Java Interview questions:

Java.lang Package      |       OOPS concepts      |       Exception handling






No comments: