Java: Access Modifiers

What is an Access modifier?
Access modifiers are keywords used to specify the accessibility of a class (or type) and its members. These modifiers can be used from code inside or outside the current application. 

  • Private
  • Default
  • Protected
  • Public

Private: Data members and methods that are declared private are used on within that specific class it was declared in. A private keyword is used to declared a data member or a method private.
Syntax: private type variable;

Default: The data members, methods can only be used within the same class or a the package where it was declared in.When a data member or a method isn’t declared with an access modifier, it will be a default access modifier.
Syntax: type variable;

Protected: The data members and methods using the protected keyword can only be accessed within the same package and any subclasses.
Syntax: protected type variable;

Public: The data members and methods can be accessible everywhere throughout the program.
Syntax: public type variable;

Non Access Modifers

  • Static – A static variable belongs to a class rather than an instance of a class.
  • Final – A final variable is basically unchangeable.
  • Abstract – Abstract classes cannot be instantiated but they can be subclassed. They are basically used as templates for other classes.

Example Program – Public

abstract class Dog{
    abstract void bark();
}

class Golden_Retreiver extends Dog{
    static String dad_name = "Melo";
    protected String dad_Species = "Golden Retriver";
    public int age = 10;
    void bark()
    {
        System.out.println("Bow Wow");
    }
}

public class Golden_Doodle{
    Golden_Retreiver gr = new Golden_Retreiver();
    public String name = "Kobe";
    String my_dad_name = Golden_Retreiver.dad_name;           
    int dad_age = gr.age;

    void print(){
        System.out.println("Dad's age:" + dad_age);
    }
        public static void main(String[] args) {
            Golden_Doodle kobe = new Golden_Doodle();
            Golden_Retreiver gr = new Golden_Retreiver();
            kobe.print();
            gr.age = 15;
            kobe.dad_age = gr.age;
            System.out.println("Dad's age after 5 years:" + kobe.dad_age);
        }
}

Example Program – Private

abstract class Dog{
    abstract void bark();
}

class Poodle extends Dog{
    protected String mom_name = "kai";
    protected String mom_species = "Poodle";
    private int age = 9;
    void bark()
    {
        System.out.println("wow Wow wow wow wow");
    }
}

public class Golden_Doodle extends Poodle{
    Golden_Retreiver gr = new Golden_Retreiver();
    public String name = "Kobe";
                        
    //Accessing the Private attribute (Would Generate an error)
    int mom_age = super.age;                                
   
        public static void main(String[] args) {
           
        }
}

Example Program – Protected

abstract class Dog{
    abstract void bark();
}

class Poodle extends Dog{
    protected String mom_name = "kai";
    protected String mom_species = "Poodle";
    private int age = 9;
    void bark()
    {
        System.out.println("wow Wow wow wow wow");
    }
}

public class Golden_Doodle extends Poodle{
    Golden_Retreiver gr = new Golden_Retreiver();
    public String name = "Kobe";
    //Accessing the protected attribute
    String my_mom_name = mom_name;                            
    
    //Accessing the protected attribute
    String my_mom_species = mom_species;                      
                                 
    void print(){
        System.out.println("Dog's Name: " + name + "\n" + "Mom's name: "+ my_mom_name + "\n" + "Mom's Species: " + my_mom_species);
        System.out.println("Dad's name:" + my_dad_name);
        System.out.println("Dad's age:" + dad_age);
    }
        public static void main(String[] args) {
            Golden_Doodle kobe = new Golden_Doodle();
            Golden_Retreiver gr = new Golden_Retreiver();
            kobe.print();
            gr.age = 15;
            kobe.dad_age = gr.age;
            System.out.println("Dad's age after 5 years:" + kobe.dad_age);
        }
}

Example Program – Final

abstract class Dog{
    abstract void bark();
}

class Golden_Retreiver extends Dog{
    static String dad_name = "Melo";
    protected String dad_Species = "Golden Retriver";
    final int age = 10;
    void bark()
    {
        System.out.println("Bow Wow");
    }
}

public class Golden_Doodle{
    Golden_Retreiver gr = new Golden_Retreiver();
    public String name = "Kobe";
    
    void print(){
        System.out.println("Dad's age:" + dad_age);
    }
        public static void main(String[] args) {
            Golden_Doodle kobe = new Golden_Doodle();
            Golden_Retreiver gr = new Golden_Retreiver();
            kobe.print();
            gr.age = 15;
            kobe.dad_age = gr.age;
            System.out.println("Dad's age after 5 years:" + kobe.dad_age);
        }
}

Example Program – Static

abstract class Dog{
    abstract void bark();
}

class Golden_Retreiver extends Dog{
    static String dad_name = "Melo";
    protected String dad_Species = "Golden Retriver";
    public int age = 10;
    void bark()
    {
        System.out.println("Bow Wow");
    }
}


public class Golden_Doodle extends Poodle{
    Golden_Retreiver gr = new Golden_Retreiver();
    public String name = "Kobe";
    
    String my_dad_name = Golden_Retreiver.dad_name;           
    int dad_age = gr.age;

    void print(){
        System.out.println("Dad's name:" + my_dad_name);
    }
        public static void main(String[] args) {
            Golden_Doodle kobe = new Golden_Doodle();
            kobe.print();
            }
}

Conclusion

Basically, access modifiers are used to restrict access to data members and classes.

Leave a comment

Design a site like this with WordPress.com
Get started