Java- Data Hiding/ Chapter -1 / OOP's Concept



Data Hiding

Java Data Hiding:-


In Java Data Hiding means outside person cannot access our internal data directly.
                                                or
Our internal data shouldn't go out directly this OOP's feature is called as "Data Hiding".

After validation or identification outside person can access our internal data.  

example 1: -After entering valid username and password we can able to access our Gmail account information.

example 2: -Even though we are the valid customer of the bank, we can able to access our account information and we can't access other's account information.

               

                HOW WE CAN ACHIEVE DATA HIDING IN JAVA




Java- Data Hiding/ Chapter -1 / OOP's Concept

Data Hiding:-

Outside person cannot access our internal data directly.
                                    or
Our internal data shouldn't go out directly this OOP's feature is called as "Data Hiding".

After validation or identification outside person can access our internal data.  

example 1: -After entering valid username and password we can able to access our Gmail account information.

example 2: -Even though we are the valid customer of the bank, we can able to access our account information and we can't access other's account information.

                  HOW ACHIEVE DATA HIDING

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:-

Java- continue keyword / Chapter -8 / Flow Control


java continue keywordjava continue keyword :-



  • we can use continue keyword inside loops to skip current iteration and continue for the next iteration.


example:-
for(int i=0 ; i<10 ; i++)
{
if(i%2==0)
continue;
System.out.println(i);
}
Output is:

Java- continue keyword / Chapter -8 / Flow Control

javabychetan.blogspot.com
2. continue :-
  • we can use continue statement inside loops to skip current iteration and continue for the next iteration.
example:-
for(int i=0 ; i<10 ; i++)
{
            if(i%2==0)
            continue;
             System.out.println(i);
}
Output is:

1
3
5
7
9



  • We can use continue statement only inside a loop is we are using anywhere else, we will get compile time error saying "continue outside of loop".
example:-

Java- break keyword / Chapter -7 / Flow Control



Java break keyword


The break keyword is used to break the control flow from switch & loops.


We can use break keyword in following places.


  • Inside switch ( ) to stop fall through.


example:
int x=0

switch(x)

{

  case 0 :

              System.out.println(0);

case 1 :
System.out.println(1);
break;
case 2 :
System.out.println(2);

default : System.out.println(default);
}
Output is :   0
1

  • Inside loop to break loop execution based on some condition.


example:-

Java- break keyword / Chapter -7 / Flow Control

javabychetan.blogspot.com
3.Transfer Statement:-
1.break:-
break is used to break the control flow from switch & loops
We can  use break keyword in following places.


  • Inside switch ( ) to stop fall through.

example:
int x=0
switch(x)
{
  case 0 :
              System.out.println(0);
  case 1 :
              System.out.println(1);
                                  break;
  case 2 :
              System.out.println(2);

default : System.out.println(default);
}
Output is :   0
                   1


  • Inside loop to break loop execution based on some condition.
example:-

Java- for each loop / Chapter -6 / Flow Control (1.5v)


java for each loop

java for each loop came into 1.5 version.


It is the specially designed loop to retrieve elements of arrays and collections.


example 1 :-
Q. print elements of the one-dimensional array?
int [ ] a={1,2,3,4,5};
To prints the elements of the array, we have 2 approaches.

1.old approach (old for loop) :-

for(int i=0 ; i<a.lenght ;i++ )
{
System.out.ptintln(a[i]);
}                                                   Output is  :
1
2
3
4
5

2. new approach (for each loop):-

for(int a1 :a )
{
System.out.ptintln(a1);
}

Output is :

Java- for each loop / Chapter -6 / Flow Control (1.5v)

javabychetan.blogspot.com
for each loop came into 1.5 version.It is specially designed loop to retrieve elements of arrays and collections.
example 1 :-
Q. print elements of the one-dimensional array?
 int [ ] a={1,2,3,4,5};
 To prints the elements of the array, we have 2 approaches.

1.old approach (old for loop) :-

for(int i=0 ; i<a.lenght ;i++ )              
{                                                      
System.out.ptintln(a[i]);
  }                                                   Output is  :
                                                                1            
                                                                2
                                                                3
                                                                4
                                                                5

2. new approach (for each loop):-

for(int a1 :a )
 {
System.out.ptintln(a1);                              
}      

Output is :  

Java for loop / Chapter -5/Flow Control




java for loop:-



  • If we know the number of iterations in advance then we should use for ( ) loop. 

  • It is the most commonly used loop in java.

  • Curly braces{ } are optional and without Curly braces { } we can take one statement under while which should not be the declarative statement.


Syntax:-
java for loop

                                                
(a) initialization section;-


Java-for( ) loop / Chapter -5/Flow Control




c. for ( ) loop :- 

  • If we know the number of iterations in advance then we should use for ( ) loop. 
  • It is the most commonly used loop in java.
  • Curly braces{ } are optional and without Curly braces { } we can take one statement under while which should not be the declarative statement.


Syntax:-
javabychetan.blogspot.com
                                                
(a) initialization section;-


  • This part will be executed only once in the whole life cycle of the for() loop.

Java do while loop / Chapter -4/ Flow Control




Java do while loop

Java do while loop:-


If we want to execute loop body at least once then we should go for the do-while loop.

Syntax:-

do
{
         body
}  while (b);         

                         Where; the semicolon is mandatory and b always should be a boolean type.



1. The arguments should be a boolean type. If we are trying to provide any other type then we will get compile time error.

example:
int b=10;
do
{
         body

}  while (b); 

Compile Time Error: Incompatible Type
Found: int
Required: boolean

Java-do-while loop / Chapter -4/Flow Control




b). do -while loop:-
If we want to execute loop body at-least once then we should go for the do-while loop.

Syntax:-

do
{
         body
}  while (b);         

                         where ; the semicolon is mandatory and b always should be a boolean type.



1. The arguments should be a boolean type . If we are trying to provide any other type then we will get compile time error.

example:
int b=10;
do
{
         body

}  while (b); 

Compile Time Error: Incompatible Type
Found:int
Required:boolean

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com