Total members 11893 |It is currently Tue Nov 05, 2024 11:29 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Access Modifiers in Java
There are four access modifiers in java which control the visibility of classes, interfaces, enums, functions and variables:

  • Default Access Modifier


    You don't have to add access modifiers to your classes, functions and variables. In this case you will be using the default access modifier. In this case your defined classes, functions and variables will be visible only within the classes in the same java package.
    java code
    class ExampleClass {
        int x;
        int y;
        int sum;

         ExampleClass() {
             sum=x+y;
        }
        

  • Public Access Modifier


    When using public access modifier your classes,functions and variables will be visible in same to classes in the same package and any external packages. In brief It can be access from any Java class but sure will have to import to at the header.
    java code
    public class ExampleClass {
        public int x;
        public int y;
        public  int sum;

        public  ExampleClass() {
             sum=x+y;
        }
        
    }
     

  • Protected Access Modifier


    When using protected access modifier your class members will be visible within the same class and one that inherit from it(child class). The child class is allowed to overload and override the parent class protected functions. See Example below:
    java code
     class ExampleClass {
        protected int x;
        protected int y;
        protected  int sum;

        protected  ExampleClass() {
             sum=x+y;
        }
        void setY()
        {
            y=3;
        }
        
    }
    class  ExampleInherit extends ExampleClass
    {
        void setX()
        {
            x=4;
            setY();
        }
        void setY()
        {
            y=544;
        }
        void setY(int decimal)
        {
            y=decimal;
        }
    }
     
     

    Please note the following about protected access modifiers:
    •  Classes and Interfaces can't be declared using protected access modifiers.
    •  You can declare inner classes using protected modifier:
      java code
      class ExampleClass {
          protected int x;
          protected int y;
          protected  int sum;

          protected  ExampleClass() {
               sum=x+y;
          }
          protected class ExampleInnerClass{
           
          }
          void setY()
          {
              y=3;
          }
          
      }
       
    •  Protected access modifier can't be used for interfaces members.
     
  • Private Access Modifier


    Private access modifier is most restricted access modifier in java. It allows you only to access class members within the same class.
    java code
    class ExampleClass {
        private int x;
        private int y;
        private  int sum;

        protected  ExampleClass() {
             sum=x+y;
        }

        public void setX(int x) {
            this.x = x;
        }

        public void setY(int y) {
            this.y = y;
        }
         
        
       
    }
    class  ExampleInherit extends ExampleClass
    {
        
        void setX()
        {
            setX(3);
            setY(4);
        }
    }
     

    • Classes and interfaces can't be declared using private access modifiers.
    • Inner classes can be declared using private modifier because in this case they would be a class member.
      java code
      class ExampleClass {
          private int x;
          private int y;
          private  int sum;

          protected  ExampleClass() {
               sum=x+y;
          }

          public void setX(int x) {
              this.x = x;
          }

          public void setY(int y) {
              this.y = y;
          }
          
          private class ExampleInnerClass{
           
          }

      }
       
    • Interface members can't be declared using private access modifier.
    • Child class can't access parent class private members but still can overrides its functions.
    Private Constructor:
    If you declared the class constructor as private will not able to create instance of this class outside the its scope. For example :
    This code works fine( Inside the scope)
    java code
    public class ExampleClass {
        

        private  ExampleClass() {
              
        }
        public static void main(String[] args) {
         ExampleClass ec=new ExampleClass(); 
        }
    }
     

    This code will not work ( outside the class scope):
    java code
    public class Main {

        public static void main(String[] args) {
        ExampleClass2 exampleClass2= new ExampleClass2(); // Private Constructor Error
    }
    }

      class ExampleClass2 {
        

        private  ExampleClass2() {
              
        }

    }

    Private constructors are usually used design patterns such as singleton
    [code2=java]
    public class Main {

        public static void main(String[] args) {
        ExampleClass2 exampleClass2= ExampleClass2.getInstance();
    }
    }

      class ExampleClass2 {
        private static ExampleClass2 exampleClass2;

        private  ExampleClass2() {
              
        }
        public static ExampleClass2 getInstance()
        {
            if(exampleClass2==null)
            {
                exampleClass2= new ExampleClass2();
            }
            return exampleClass2;
        }

    }
     

    Where you allow only one instance to be created from a specific class.





Author:

updated.

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : access modifiers in java
 Access modifiers     -  
 class is declared without any access modifiers     -  
 How java access URL     -  
 MS-Access with java     -  
 data access layer for java     -  
 HOW I Access the Parallel Port in Java?     -  
 correct syntax for access providers in java     -  
 java code to open word doc attached in ms access.db     -  
 Password Security Manager Access File Java     -  
 Example of using access key with links     -  



Topic Tags

Java Basics






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com