Harry Henriques wrote:Does this mean that it isn't possible to override a protected superclass method when the overriding method is in another package?
Java Language Specification wrote:6.6.2 Details on protected Access
A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.
Henry Wong wrote:Of course, it is possible to override protected methods. The methods of the subclass can access it protected superclass, via a call with the super.doThis() construct.
Java Language Specification wrote:
6.6.2 Details on protected Access
A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.
If a method is overridden but you use a polymorphic (supertype)
reference to refer to the subtype object with the overriding method, the compiler
assumes you’re calling the supertype version of the method. (from K & B)
SCJP 6
Valentin Ivanov wrote:
did you try to invoke super.doThis() from an instance method and not from main? After all the method is protected and not static protected.
a alph wrote:
Can you please explain why it doesn't print anything??
K&B wrote: If a method is overridden but you use a polymorphic (supertype)
reference to refer to the subtype object with the overriding method, the compiler
assumes you’re calling the supertype version of the method.
S Ali wrote:
in this case the superclass method was declared protected which means that it can't be accessed through a reference variable even if you override it with public access the compiler still sees the method interface of the reference variable type. Hope that answers it.
a alph wrote:
Hi
I understand that the protected members can be accessed only through inheritance.
Can you please explain to me how to access the protected method by using super.doThis() ?
When I insert the super.doThis() in the following code, it says "non static variabe super cannot be referenced from a static context".
The output of the code below is at the end of this post in RED.
In this case, the protected superclass method is referenced by a superclass reference (this is what the compiler sees), but the method with public access in the subclass is invoked polymorphically at runtime. The subclass method with public access overrides the protected superclass method (polymorphically). Both the superclass and the subclass are in the same package (in the same file). This doesn't happen when the protected superclass method is in one package and the public subclass method is in another package. In fact, a compiler error message is generated. Overriding a method is a Polymorphic concept, not an Inheritance concept. Without polymorphism, the concept of overriding a method doesn't make sense.
SCJP 6
S Ali wrote:
The subclass method with public access does override the superclass's method at runtime.But only the implementation is overriden ,the method declaration is still seen of the type of the reference variable. Your example worked because protected access inside the same package is just like default, it can be accessed through a reference.
Another example to demonstrate the concept from K&B page 108: If the supertype version
declares a checked exception, but the overriding subtype method does not, the compiler
still thinks you are calling a method that declares an exception and you are required to handle it.
Harry Henriques wrote:I don't believe that you are correct. If the subclass method overrides the superclass method, then I wouldn't get a compiler error in my original code (in the original post at the top of this thread). When the superclass method and subclass method are in different packages (directories), the subclass method doesn't override the superclass method. The subclass inherits from the superclass, and that's it.
Regards
Salil Verma
Harry Henriques wrote:This works, but I'm not sure that I understand just why it works. Can you explain it further, Henry?
Regards
Salil Verma
K&B SCJP 5 Study Guide pg. 34 wrote:
So if the subclass-outside-the-package gets a reference to the superclass (by, for example, creating an instance of the superclass somewhere in the subclass' code), the subclass cannot use the dot operator on the superclass reference to access the protected member. To a subclass-outside-the-package, a protected member might as well be default (or even private), when the subclass is using a reference to the superclass. The subclass can see the protected member only through inheritance.
- When does the polymorphic method gets called?
When was the error displayed for the code(compile time or runtime)?
Regards
Salil Verma
A wop bop a lu bop a womp bam boom! Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
|