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



If a member declared as default then we can access that member only within the current package i.e. from outside of the package we can't access.
Hence default access is also known as package level access.


3.private members:-



  • If a member is private then we can access that member only within the class i.e. from outside of the class we can't access.

  • abstract methods should be available to the child classes to provide implementation, whereas private methods are not available to the child classes to provide the implementation. Hence private abstract combination is illegal from methods.  


 

4.protected members;-




  • If a member declared as protected then we can access that member anywhere in the current package but only in child classes of outside package.

  • We can access protected members within the current package anywhere either by using its or by using child reference but we can access protected members in outside package only in child classes and we should use "child reference only".i.e. parent reference cannot be used to access protected members from outside package.


example 1.            within the same package 



package pack1;

public class A

{

    protected void m1( )

    {

        System.out.println("protected member");

     }

}

 

class B extends A

{

         public static void main(String args[])

         {

                A a =new A( );

                 a.m1();

                B b =new B( );

                 b.m1();

                A a1 =new B( );

                 a1.m1();

           }

}

 

javac -d. B.java

java pack1.B

 

Output:-

protected member

protected member

protected member

 

example 2.            outside the package 


package pack1;

public class A

{

    protected void m1( )

    {

        System.out.println("protected member");

     }

}

 



package pack2;

import pack1.A;

class C extends A

{

        public static void main(String args[])

        {

              A     a   =new A( );

              a.m1( );                    Compile Time Error

                                                                                                    m1( )has protected access in pack1.A


             C   c   =new   C( );

                  c.m1( );                 valid  o/p is: protected member

 

             A   a1 =new  C  ( );

                       a1.m1( );            Compile Time Error

          }                                     m1( )has protected access in pack1.A

}

 



  • We can access protected members from outside package only in the child class and we should use that "child class reference only".


No comments:

Post a Comment

Be the first to comment!

Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.

Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.

Regards,
JavaByChetan
Back To Home

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com