Monday, October 3, 2022

Access Modifiers

 Access modifiers determine whether other classes can use a particular field or invoke a particular method can be public, private, protected, or default ( no modifier ). See the table given below :

Access Modifierwithin classwithin packageoutside package by subclass onlyoutside package
publicYYYY
protectedYYYN
DefaultYYNN
privateYNNN



Code as described:

package com.company;

class C1{
    public int x = 5;
    protected int y =45;
    int z = 6;
    private int a = 78;
    public void meth1(){
        System.out.println(x);
        System.out.println(y);
        System.out.println(z);
        System.out.println(a);
    }
}
public class cwh_66_access_modifiers {
    public static void main(String[] args) {
        C1 c = new C1();
        // c.meth1();
        System.out.println(c.x);
        System.out.println(c.y);
        System.out.println(c.z);
        // System.out.println(c.a);
    }
}


No comments:

Post a Comment

techguruspeaks

  Core Java programming Introduction to Java Java Features Writing a Simple Java Program Java Keywords and Comments Variables, Identifiers &...