Class Level Modifier Part 2

Class Level Modifier Part 2

                                                        strictfp(strict floating point) 

  • introduced in 1.2v .
  • we can declare strictfp for classes and methods but not for variables.
  • usually, the result of floating point arithmetic is varied from platform to a platform if we want to be platform-independent for floating arithmetic then we should go for strictfp modifier.
strictfp method-:if a method is declared as strictfp all floating point calculation in that method has to follow IEEE 754 standard so that we will get a platform-independent result.
abstract modifier never talks about implementation where as strictfp method always talks about implementation hence abstract strictfp combination is illegal for methods.

eg-:
public abstract strictfp void m();//is invalid

compile time error-: illegal combination of modifier abstract and strictfp

strictfp class-:if a class is declared as strictfp then every floating calculation present in every concrete method has to follow IEEE 754 standards so that we will get the platform-independent result.we can declare abstract strictfp combination for the class abstract strictfp combination is legal for class but illegal for method.

eg-:

abstract strictfp class Test{
}


                                   Member Modifier(method or variable level modifiers)

public members-:if a member is declared as public then we can access that member from anywhere but the corresponding class should be visible that is before checking member visibility we have to check class visibility.

eg-:

package pack1;
class A{
 public void m(){
      System.out.print("Hello");
}
}
package pack2
import pack1.A;
class B{
   public static void main(String... args){
        A a=new A();
        a.m();
  }
}

compile time error-: pack1.A is not public in pack1 cannot access to outside package.

in the above example even though m() is public we can't access from the outside package because a corresponding class is not public that is if both class and method then only we can access outside of the package.

defaults member-:if a member declares as default then we can access that member only within the current package from outside of the package we can't access hence pack default access is also known as package level access.

private members-: if a member is private then we can access members only within the class that is from outside we can't access.
abstract method should be available to the child class to provide implementation whereas private methods are not available to the child classes hence private abstract combination is illegal for methods.


protected members-:  if a member is declared protected, then we can access that member anywhere within the current package but only in a child class outside of the package.

we can access protected members within the current package anywhere either by using parent reference or by using child reference 

but we can access protected in outside package only in child classes and we should use child references only that is parent references can't be used to access protected members from the outside package.





protected =<default>+child

eg-:

package pack1;
public class A{
    protected void m1(){
           System.out.print("Hello");
    }
}
class B extends A{
        public static void main(String... args){
             A a=new A();
            a.m1();                                     //valid
            B b=new B();
            b.m1();                                    // valid
            A aa=new B();
            aa.m1();                                  //valid
        }
}

package pack2;
import pacak1.A;
class C extends A{
        public static void main(String... args){
           A a=new A();
            a.m1();                                 //inValid
          C c=new C();                        
           c.m1();                                 //Valid
          A ac=new C();
          ac.m1();                                //inValid
       }
}


we can access protected members from the outside package only in child classes and we should use that child class reference only.
for example-:
from d class if we want to access we should use D class reference only.


package pack1;
public class A{
    protected void m1(){
           System.out.print("Hello");
    }
}
class B extends A{
        public static void main(String... args){
             A a=new A();
            a.m1();                                     //valid
            B b=new B();
            b.m1();                                    // valid
            A aa=new B();
            aa.m1();                                  //valid
        }
}

package pack2;
class C extends A{
}
class D extends C{
        public static void main(String... args){
            A a=new A();
            a.m1();                //inValid
           A ac=new C();
           ac.m1();                //inValid
           A a1=new D();
           a1.m1();                //inValid
          C c=new C();
          c.m1();                    //inValid
          C c1=new D();
          c1.m1();                  //inValid
         D d=new D();
         d.m1();                    //Valid
     }
}
compile time error-: m1() has protected access in pack1.A

Summary Table-:
the most restricted accessible modifier is private.
the most accessible modifier is public.
recommended modifier for data members (variable is private) but recommended for methods is public.
private<default<protected<public

Visibility

public

private

protected

default

Within the  same class

Allowed

Allowed

Allowed

Allowed

From child class of the same package

Allowed

Not Allowed

Allowed

Allowed

From the non-child class of the same package

Allowed

Not Allowed

Allowed

Allowed

From child class of outside package

Allowed

Not Allowed

Allowed

We should use child reference only

Not Allowed

From the non-child class outside package

Allowed

Not Allowed

Not Allowed

Not Allowed



Previous Post Next Post

Most Recent