Use of Protected Access Specifier



Define a Super
Class - A with protected instance variable, B child Class of A with private
variable and try to access those variable from Class C, child of B.





class A


 {


  protected int i=10;


  void Display()


    {


     System.out.println("Class A"+i);


    }


 }


class B extends A


 {


  private int j=20;


  void Display()


    {


     super.Display();


     System.out.println("Class B" +i
+" "+j);


    }


 }


class C extends B


 {


  int k=30;


  void Display()


    {


     super.Display();


     System.out.println("Class C" +i
+" "+j+ " "+k);


    }


 }


class Sk16


 {


  public static void main(String args[])


   {


    C obj=new C();


    obj.Display();


    }


 }


Compile:


D:\>javac
Sk16.java


Error:


D:\SK>javac
Sk16.java


Sk16.java:31:
j has private access in B


     System.out.println("Class C" +i
+" "+j+ " "+k);


                                          ^




1
error


0 Comments