Access Control in java programs

This topic is access control in java programs with public, private, protected and non-modifier. This is biggest blogs and video.

Limit of Access Control Modifier

ModifierAccess Limit
PrivateWork only in own class
Non-ModifierWork only in one package
ProtectedWork in one package, work in different package if two class are inheritance
PublicWork in all place

 

Classes and Packages List use in this programs.

Package NameClass Name
Pack1A.java, B.java and C.java
Pack2D.java, E.java
AccessPackExample1.java

 


Video available on YouTube.

 


 

Example on Access Control with Modifiers

Class A in Pack1 Package

package pack1;

public class A

{

            int x = 10;

            public int x_pub= 20;

            private int x_pri = 30;

            protected int x_pro = 40;

           

            // use in same class

            public void methodA()

            {

                        System.out.println(“X = ” + x);

                        System.out.println(“X Pri = ” + x_pri);

                        System.out.println(“X Pro = ” + x_pro);

                        System.out.println(“X Pub = ” + x_pub);

            }

}


Class B in Pack1 Package

package pack1;

public class B extends A

{

            public void methodB()

            {

                        System.out.println(“X = ” + x);

                        // System.out.println(“X Pri = ” + x_pri);

                        System.out.println(“X Pro = ” + x_pro);

                        System.out.println(“X Pub = ” + x_pub);

            }

            /*

             * Private variable can’t able to access from other class

             */

}


Class C in Pack1 Package

package pack1;

public class C

{

                public void methodC()

                {

                                // Create A class object

                                A temp = new A();

                                System.out.println(“X = ” + temp.x);

                                //System.out.println(“X Pri = ” + temp.x_pri);

                                System.out.println(“X Pro = ” + temp.x_pro);

                                System.out.println(“X Pub = ” + temp.x_pub);

                }

}


Class D in Pack2 Package

package pack2;

import pack1.*;                // Import pack1 in this java file

public class D extends A

{

                public void methodD()

                {

                                //System.out.println(“X = ” + x);

                                //System.out.println(“X Pri = ” + x_pri);

                                System.out.println(“X Pro = ” + x_pro);

                                System.out.println(“X Pub = ” + x_pub);

                }

}


Class E in Pack2 Package

package pack2;

import pack1.A;                // import A class in this program

public class E

{

                public void methodE()

                {

                                // Create A class object

                                A temp = new A();

                                //System.out.println(“X = ” + temp.x);

                                //System.out.println(“X Pri = ” + temp.x_pri);

                                //System.out.println(“X Pro = ” + temp.x_pro);

                                System.out.println(“X Pub = ” + temp.x_pub);

                }

}


Class Example1 in AccessPack Package

package AccessPack;

import pack1.*; // import pack1 all class in example

import pack2.*; // import pack2 all class in example

public class Example1

{

                public static void main(String[] args)

                {

                                A objA = new A();

                                objA.methodA();       // All variable can access in class

 

                                B objB = new B();

                                objB.methodB();       // private can’t access here

 

                                C objC = new C();

                                objC.methodC();      // private can’t access here

 

                                // Different Package

                                D objD = new D();

                                objD.methodD();    // public and protected are working

 

                                E objE = new E();

                                objE.methodE();   // only public can access here

                }

}


How to compile this program in command prompt

Javac AccessPack/Example1.java

How to run this program in command prompt

Java AccessPack.Example1

 

Output:

OutPut1:

X = 10

X Pri = 30

X Pro = 40

X Pub = 20


OutPut2:

X = 10

X Pro = 40

X Pub = 20


OutPut3:

X = 10

X Pro = 40

X Pub = 20


OutPut4:

X Pro = 40

X Pub = 20


OutPut5:

X Pub = 20


[sbscrbr_form]

If you have any query on blog example, you can directly comment us or subscribe our website

 

Earn Money Online

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

658total visits,1visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

EmailEmail
PrintPrint