Recently I was practicing inheritance topic in Java. I came across this statement that "protected member variables in super class are private in a subclass".
My question is, if a sub class itself is a parent of another subclass, will that subclass be able to access the member variables of the base class?
public class A
{
protected int a;
public A(int a)
{
this.a = a;
}
}
public class B extends A
{
public B(int a)
{
super(a);
}
}
public class C extends B
{
public C(int a)
{
super(a);
}
@Override
public String toString()
{
return ("" + this.a);
}
}
public class Test
{
public static void main(String args[])
{
C c = new C(5);
System.out.println(c);
}
}
Would C, a sub-sub-class of A, be able to access a, since by the above logic, protected becomes private in B and private variables cannot be inherited?
Testing this does print 5, how does this work then?