Java Program to Demonstrate the Protected Access Specifier

You are currently viewing Java Program to Demonstrate the Protected Access Specifier

Java program to demonstrate the protected access specifier

// Java program to demonstrate the 
// protected access specifier

class A {
  protected void MethodA() {
    System.out.println("MethodA called");
  }
}

class B extends A {
  protected void MethodB() {
    System.out.println("MethodB called");
  }
}

class C extends B {
  protected void MethodC() {
    System.out.println("MethodC called");
  }
}

public class Main {
  public static void main(String[] args) {
    C obj = new C();

    obj.MethodA();
    obj.MethodB();
    obj.MethodC();
  }
}

Output

MethodA called
MethodB called
MethodC called

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply