Protected methods are inherited by subclasses, so the subclass can invoke its own inherited test() method. However, this still doesn't give it the right to invoke its parent's test() method using a parent reference.
In other words, if ClassA has a protected method myMethod, a class in a different package can never invoke myMethod() using a ClassA reference. This is true even if the other class is a subclass of ClassA. Being a subclass only gives it the right to invoke myMethod() using a reference to its own class type--not the parent's type.
It works when you remove the package declarations because this then puts both classes in the same package--and protected methods are always visible to all classes in the same package. [ October 29, 2007: Message edited by: Kelvin Lim ]
I think the first problem is, that the class Parent is not declared public. It is not visible in other packages and therefore can not be inherited from in other packages.
Kelvin Chenhao Lim
posted 12 years ago
Originally posted by Marcus Didius Falco: I think the first problem is, that the class Parent is not declared public. It is not visible in other packages and therefore can not be inherited from in other packages.
Good catch! I was blind-sided by the fact that Adam specifically said that "Line 1 doesn't compile". So, um, yeah, fix the class visibility first, and then read my earlier comment.
Hi adam, In the above code you are trying to create an object of the superclass and access the members of the superclass but it is not possible, we can refer the super class members only through inheritance but not through the superclass's object from subclass.