Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Access Modifiers in Java

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 424
    Comment on it

    Access Modifiers:

    Access Modifiers are used to provide access level and visibility to variables,classes and to methods. There are four access modifiers in Java.

    1. Default
    2. Private
    3. Public
    4. Protected

    1. Default Access Modifier:

    • If we do not provide any access specifier then the variable or class or method will have default access level. Default access modifier provide access level and visibility within a package only. It means that we can access a variable or method or class within a same package only.
    • No keyword is used for default access modifier.

    2. Private Access Modifier:

    • To set private access level we use private keyword in front of a class or a variable or a method. Methods and variables declared private are can be accessed within a same class in which they are declared.

    Example:

    public class PrivateAccess {
       private int a=10;
       System.out.println("the value of a is " +a);
    
    }
    

    3. Public Access Modifier

    • To set public access level we use public keyword in front of a class or a variable or a method. Methods and variables declared public can be accessed from any class even outside of a package.

    4.Protected Access Modifier:

    • To set protected access level we use protected keyword in front of a class or a variable or a method. Methods and variables declared protected can be accessed within the package and also by subclasses outside a package which extends protected class.
    • Protected access specifier are mainly used in case of inheritance.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: