Showing posts with label Access Modifier. Show all posts
Showing posts with label Access Modifier. Show all posts

Java- volatile modifier / Chapter -10 / Access Modifier




Java Volatile modifier


1." volatile" is a modifier applicable only for variable and we can't apply for methods & classes.

2.If a value of a variable keeps on changing by multiple threads then there may be a chance of data inconsistency problem.We can solve this problem by using "volatile modifier".

3.If a variable declared as volatile then for every thread JVM will create a separate local copy.



Java- volatile modifier / Chapter -10 / Access Modifier


  1. "volatile" is a modifier applicable only for variable and we can't apply for methods & classes.
  2. If a value of a variable keep on changing by multiple threads then there may be a chance of data inconsistency problem.We can solve this problem by using "volatile modifier".
  3. If a variable declared as volatile then for every thread JVM will create a separate local copy.

Java- transient modifier / Chapter -9 / Access Modifier


java transient modifier

Java transient modifier


1. transient is the modifier applicable only for the variable.

2. We can use the transient keyword in serialization context.

3. At the time of serialization if we don't want to save the value of a particular variable to meet security constraint then we should declare that variable as transient.

 


Java- transient modifier / Chapter -9 / Access Modifier


  1. transient is the modifier applicable only for the variable.
  2. We can use the transient keyword in serialization context.
  3. At the time of serialization if we don't want to save the value of a particular variable to meet security constraint then we should declare that variable as transient.

Java- native modifier / Chapter -8 / Access Modifier

java native modifier

java native modifier


1. "native" is a modifier applicable only for methods and we cannot apply anywhere else. 2. The methods which are implemented in non-java (C or C++) are called native methods. 3. The main objective of native modifier are:-



  • Improve performance of the system.

  • To achieve machine level or memory level communication.

  • To use already existing legacy (non-java) code.



Java- native modifier / Chapter -8 / Access Modifier



1. "native" is a modifier applicable only for methods and we cannot apply anywhere else.
2. The methods which are implemented in non-java (C or C++) are called native methods.
3. The main objective of native modifier are:-

  • Improve performance of the system.
  • To achieve machine level or memory level communication.
  • To use already existing legacy (non-java) code.

Java- synchronized modifier / Chapter -7/ Access Modifier


java synchronized modifier

 Java synchronized  modifier


1." synchronized" is a modifier applicable for methods and blocks but not for classes and variables.

2.If multiple threads are trying to operate simultaneously on the same java object then, there may be a chance of data inconsistency problem.This is called "Race Condition".We can overcome this problem by using synchronized keyword or modifier.


Java- synchronized modifier / Chapter -7/ Access Modifier

  1. synchronized is a modifier applicable for methods and blocks but not for classes and variables.
  2. If multiple threads are trying to operate simultaneously on the same java object then, there may be a chance of data inconsistency problem.This is called "Race Condition".We can overcome this problem by using synchronized keyword or modifier.
  3. If a method or block declared as synchronized then at a time only one thread is allowed to execute that method or block on the given objects so that data inconsistency problem won't come.

Java- static modifier / Chapter -6/ Access Modifier



Java static modifier

Java static modifier




  1. "static" is a modifier applicable for methods & variables but not for classes.(not for top-level classes but for inner classes we can apply)

  2. We can't declare top level classes with static modifier but we can declare inner classes as "static".

  3. in the case of instance variables for every object a separate copy will be created but in the case of static a single copy will be created at the class level and this will be shared by every object of that class.  example:-
    class Test

    {

          static int x=10;

          int y=20;

          public static void main(String [ ]args)

          {

               Test    t1 =new   Test( );

                t1.x =  888;

                t1.y =  999;

               Test   t2 =new Test( );

               System.out.println(t2.x +".........."+t2.y);

          }

    }

     

    output is;

Java- static modifier / Chapter -6/ Access Modifier


  1. "static" is a modifier applicable for methods & variables but not for classes.(not for top-level classes but for inner classes we can apply)
  2. We can't declare top level classes with static modifier but we can declare inner classes as "static".
  3. in the case of instance variables for every object a separate copy will be created but in the case of static a single copy will be created at the class level and this will be shared by every object of that class.  example:-
    class Test
    {
          static int x=10;
          int y=20;
          public static void main(String [ ]args)
          {
               Test    t1 =new   Test( );
                t1.x =  888;
                t1.y =  999;
               Test   t2 =new Test( );
               System.out.println(t2.x +".........."+t2.y);
          }
    }

    output is;

Java- public,default,private &protected modifiers for method & variables / Chapter -5/ Access Modifier

public,default,private &protected modifiers

Java public,default,private &protected modifiers



1. public members:-


If a member(variable or method) declared as public then we can access that member from anywhere.
But corresponding class should be visible or accessible so that before checking member visibility we have to check class visibility.


example:-
public,default,private &protected modifiers

public,default,private &protected modifiers


In the above example m1() method is public but we can't access from outside package corresponding class A is not public i.e. if both classes &  method are public than only we can access the method from outside package.


2.default members:-


Java- public,default,private &protected modifiers for method & variables / Chapter -5/ Access Modifier

1. public members:-
If a member(variable or method) declared as public then we can access that member from anywhere.
But corresponding class should be visible or accessible so that before checking member visibility we have to check class visibility.

example:-

Java - strictfp modifier / Chapter -4/ Access Modifier



Java strictfp modifier



  • strictfp modifier introduced in 1.2v.

  • We can declare strictfp for classes and methods but not for variables. 
    Usually, the result floating point arithmetic is varied from platform to platform. If we want to platform independent result for floating point arithmetic then we should go for the strictfp modifier.  




    1.strictfp( ):-

    • If a method declared as strictfp( ) all floating point calculation in that method has to follow IEEE 754 standard.So that we will get platform independent results.

    • abstract modifier never talks about implementation where as strictfp method always talks about implementation hence abstract strictfp combination is illegal for methods.


Java - strictfp modifier / Chapter -4/ Access Modifier


strictfp modifier introduced in 1.2v.
We can declare strictfp for classes and methods but not for variables. 
Usually, the result floating point arithmetic is varied from platform to platform. If we want to platform independent result for floating point arithmetic then we should go for strictfp modifier.  


1.strictfp( ):-

  • If a method declared as strictfp( ) all floating point calculation in that method has to follow IEEE 754 standard.So that we will get platform independent results.
  • abstract modifier never talks about implementation where as strictfp method always talks about implementation hence abstract strictfp combination is illegal for methods.
2.strictfp( ) class :-

  • If a class declared

Java- abstract modifier / Chapter -3/ Access Modifier



Java- abstract modifier


the abstract is a modifier applicable for classes and methods but not for variables.

1 . abstract method:-
If we don't know about implementation even though we have to declare the method then we should go for abstract method.
In the abstract method, the only declaration is available but not the implementation that's why abstract method declaration should end with "; " semicolon.


example:-
public abstract void m1( ) ;           correct
public abstract void m1( ){ }          incorrect

Child class is responsible to provide implementation for parent class abstract method.

example:-

abstract class Parent
{
    public  abstract int salary() ;
}


class Child1 extends Parent
{
    public int salary( )
    {
           return 70000;   
    }
}

Java- abstract modifier / Chapter -3/ Access Modifier

abstract modifier:-
the abstract is a modifier applicable for classes and methods but not for variables.

1 . abstract method:-
If we don't about implementation even though we have to declare the method then we should go for abstract method.
In the abstract method, the only declaration is available but not the implementation that's why abstract method declaration should end with "; " semicolon.


example:
 public abstract void m1( ) ;           correct
public abstract void m1( ){ }          incorrect

Child class is responsible to provide implementation for parent class abstract method.


example:-

abstract class Parent
{
    public  abstract int salary() ;
}

Java- final modifier / Chapter -2/ Access Modifier


Java final modifier

Java final modifier


final is a modifier applicable for method, classes & variables.


1.final method:-

 

Whatever method parent class has by default available to the child class through inheritance.

If the child class is not satisfied with the parent method implementation than the child is allowed to redefine that method based on its requirement, this process is called "Overriding".

 

But, 

If the parent class method declared as final then we can't override that method in child class. Because it's implementation is final.

 

example:-

Java- final modifier / Chapter -2/ Access Modifier


final Modifier:-
the final is a modifier applicable for method, classes & variables.


1.final method:-

Whatever method parent class has by default available to the child class through inheritance.
If the child class is not satisfied with the parent method implementation than the child is allowed to redefine that method based on its requirement, this process is called "Overriding".

But, 
If the parent class method declared as final then we can't override that method in child class. Because it's implementation is final.

example:-
class Parent
{
           public void property()
         {   
              System.out.println("parent property");
        }
        public  final void marriage()
         {
                  System.out.println("parent choice girl");
        }

class Child extends Parent
{
    public void marriage()
     { 
         System.out.println("Child choice girl");
    }
}

Compile Time Error: marriage () in Child cannot override marriage () in Parent; Overridden method is final.



2. final class:-

Java- class level modifiers / Chapter -1/ Access Modifier


java Access Modifier

Access Modifier



Whenever we are writing our own classes we have to provide some information about our class to JVM like:-



  1. whether this class can be accessible from anywhere or not?

  2. Whether child class creation is possible or not?

  3. Whether the object creation is possible or not?


We can answer these questions by using an appropriate modifier.

Class Level Modifiers

 The only applicable modifiers for Top-Level Classes are:-

Java- class level modifiers / Chapter -1/ Access Modifier


Whenever we are writing our own classes we have to provide some information about our class to JVM like:-

  1. whether this class can be accessible from anywhere or not?
  2. Whether child class creation is possible or not?
  3. Whether the object creation is possible or not?
We can answer these questions by using an appropriate modifier.

Class Level Modifiers

 The only applicable modifiers for Top-Level Classes are:-

public 
<default>
final
abstract
strictfp


But for inner-classes the applicable modifiers are:-

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com