Java Access Modifiers - Private, Protected, Default and Public

Advertisements

Access modifiers in Java

Access modifiers defines the scope of class, methods, variables, enums etc.

Java supports many access modifiers such as:

  • Default
  • Private
  • Protected
  • Public

	Note : A class can be only public or default. Class can't be private or protected.

Default Access Modifier

Default means we have not specified any modifier to class, method, variable, etc. Members without modifiers can be accessed within same package.

Example


    //MyCalculator.java

    public class MyCalculator
    {
        int Add(int x,int y)
        {
            return x+y;
        }
    }


    //TestClass.java

    class TestClass
    {
        public static void main(String arg[])
        {

            MyCalculator M = new MyCalculator();  //Creating Object

            System.out.print("\n\tThe sum is : " + M.Add(10,20));//Calling Add() through Object
        }
    }

Private Access Modifier

The scope of private members are restricted to its own class. Private members can't be accessed out side the class.

Example


    //TestClass.java

    class MyCalculator
    {
        private int Add(int x,int y)
        {
            return x+y;
        }
    }

    class TestClass
    {
        public static void main(String arg[])
        {

            MyCalculator M = new MyCalculator();  //Creating Object

            System.out.print("\n\tThe sum is : " + M.Add(10,20));//Error: private access

        }
    }

Protected Access Modifier

The protected members can be accessed within package and outside package.

Within package : It can be accessed through object or by inheritance.

Outside package : It can be accessed only by inheritance.

Example 1: Within package, through object.


    //MyCalculator.java

    public class MyCalculator
    {

        protected int Add(int x,int y)
        {
            return x+y;
        }

    }


    //TestClass.java

    class TestClass
    {
        public static void main(String arg[])
        {

            MyCalculator M = new MyCalculator();  //Creating Object

            System.out.print("\n\tThe sum is : " + M.Add(10,20));
                                                //Calling Add() through Object

        }
    }

Example 2: Within package, by inheritance.


    //MyCalculator.java

    public class MyCalculator
    {
        protected int Add(int x,int y)
        {
            return x+y;
        }
    }


    //TestClass.java

    class TestClass extends MyCalculator
    {

        void Display()
        {
            System.out.print("\n\tThe sum is : " + Add(10,20));//Calling Add() method
        }

        public static void main(String arg[])
        {

            TestClass T = new TestClass();

            T.Display();

        }
    }

Example 3 : Outside package, only by inheritance.


    //MyCalculator.java inside package P1

    package P1;

    public class MyCalculator
    {
        protected int Add(int x,int y)
        {
            return x+y;
        }
    }


    //TestClass.java outside P1 package

    import P1.MyCalculator;

    class TestClass extends MyCalculator
    {

        void Display()
        {
            System.out.print("\n\tThe sum is : " + Add(10,20));//Calling Add() method
        }

        public static void main(String arg[])
        {
            TestClass T = new TestClass();

            T.Display();
        }
    }

Public Access Modifier

Public members can be accessed inside and outside package and in sub classes also.

Example of using public access modifier


    //MyCalculator.java inside package P1

    package P1;

    public class MyCalculator
    {

        public int Add(int x,int y)
        {
            return x+y;
        }

    }


    //TestClass.java outside P1 package

    import P1.MyCalculator;

    class TestClass
    {
        public static void main(String arg[])
        {

            MyCalculator M = new MyCalculator();  //Creating Object

            System.out.print("\n\tThe sum is : " + M.Add(10,20)); //Calling Add() method

        }
    }

Advertisement