Java Program using protected keyword


In this program, You will learn how to implement protected keyword in Java.


Access modifiers in java: public, private, protected and default


Example: How to implement protected keyword in Java

Run it Now
class A {
    protected int x = 10;
}

class Main extends A {

    void print() {
        System.out.println("The x value is :" + x);
    }

    public static void main(String args[]) {
        Main obj = new Main();
        obj.print();
    }
}

Output:

The x value is :10