my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Overriding and method access modifier Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Overriding and method access modifier" Watch "Doubt in Overriding and method access modifier" New topic
Author

Doubt in Overriding and method access modifier

Laura Montes
Greenhorn

Joined: Aug 03, 2007
Posts: 9
Hello everyone!!

I'm studying for the SCJP 5 exam and while doing some exercises I had a compile error with the following classes. Could anyone explain me how come is this error, please?



These is the error message:

Son.java:13: x() has protected access in inheritance.father.Father
f.x();


What I understand is that protected methods can be accesed by classes that are in the same package, also subclasses but these subclasses does not matter if they are in a different package.
With polymorphism the object type determine which method is called, not the reference type.

I thought that the variable f, which reference type is Father, is trying to access Son method x() because of polymorphism and Father does not have access to Son protected methods. But the error is telling me that the problem is with the Father method´┐Ż so I got confused about what is exactly doing in compilation time.

If I change the classes to be in the same package it works fine, also If I change the access modifier method to public works fine too.
Dave Walsh
Greenhorn

Joined: Jun 21, 2007
Posts: 24
Yeah it's because these polymorph calls are made at run time. The compiler sees that variable f is of type Father and connects f.x() to this class, sees it's in a different package and issues the error.

Again, the call to the subclass function is made at run time. K&B are quite explicit about this somewhere in a "exam watch".

Hope this helps,

Dave
[ August 03, 2007: Message edited by: Dave Walsh ]
Laura Montes
Greenhorn

Joined: Aug 03, 2007
Posts: 9
Thanks a lot!!!

After reading K&B now It's totally clear for me.

Thanks for the advice
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

Yeah it's because these polymorph calls are made at run time.


Oh, I think that I get it now. Are we talking about page 37 in K & B?

Dave Walsh
Greenhorn

Joined: Jun 21, 2007
Posts: 24
You're right, p. 37 presents the other part of the problem. We already determined why Father's x() was called.

Remember: main() is static and not part of an object instance, so not part of Father's inheritance tree. So Father's x() is called outside Father's package AND outside Father's inheritance tree. Consequently, access is forbidden by the protected modifier.

If you move both classes in the same package, access is allowed by the protected modifier even if the call is made outside Father's inheritance tree.
srinivas marasu
Ranch Hand

Joined: Mar 25, 2007
Posts: 290
Hi All,
If reference is of type Son,which is from a different package,which extends Father,accessing protected memember using Son reference variable is also compiles perfectly,just check K&B page no:34


-srinivas
Dave Walsh
Greenhorn

Joined: Jun 21, 2007
Posts: 24
You got that straight !
Marc Wentink
Ranch Hand

Joined: May 18, 2007
Posts: 142
Rereading this page I think the issue is that Son can only use the protected function of Father through inheritance and not through a variable of (runtime) Father.

Hence you could add a function to Son like:

public void y()
{
super.x();
x();
}

And you could erase the overwritten method void x() in Son and add a funtion to Son like

public void z()
{
x();
}

And a also reference of type Son in main could call y() and z() without an error.

By the way, I understand this stuff when I see the correct answer, but if I do some mock questions, I still keep on making errors. So, I get about 70% on the mock questions I can see on different web-sites. Is this good enough more or less? My exam is the tenth of august, and I am getting a bit depressed thinking it's to soon for me.



[ August 04, 2007: Message edited by: Marc Wentink ]

SCJP5
Dave Walsh
Greenhorn

Joined: Jun 21, 2007
Posts: 24
Hey Marc,

You're right, x() is only usable through inheritance, that is in a member function of Son.

About the exam, I haven't got a date set up yet but I don't expect to score too high. To me, succeeding in SCJP is mostly a matter of how much stuff you can remember by heart. I'm thinking about things like I/O API, Generics syntax, Wrapper classes methods, Collection(s) methods, tons of rules, etc.

I don't find these hard to understand at all but I know I'm not going to remember everything and score 98%. I expect to go through the exam and tell myself "god dang it (it'll probably be a different expression ) I know there's a rule about that thing, but what is it ?"

Some other stuff, you can reasonably expect to be able to deduce. For example, var args always have to be the last argument of a method because otherwise, the compiler would have a really hard time figuring out what type of data is being passed to the method.

So my plan is to finish reading the book (I have about 200 pages left), I'll go through all the 2 minutes drill, reread a few critical sections and take as many mock exams as possible. I'm telling you, If I consistently start scoring 70% and above, I'll consider myself ready.
[ August 05, 2007: Message edited by: Dave Walsh ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Overriding and method access modifier