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