This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes inheritance thru upcasted reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "inheritance thru upcasted reference" Watch "inheritance thru upcasted reference" New topic
Author

inheritance thru upcasted reference

william kane
Ranch Hand

Joined: Nov 21, 2000
Posts: 260
BaseClass bc = new DerivedClass();
bc.protectedMethod();
In the above given code the BaseClass is the parent of derived class and protectedMethod is protected in Base Class
And this code is placed in a instance method of the derived class.
Why do i get a compiler error ........ " Can't access protected method protectedMehod in BaseClass. BaseClass is not a subclass of the current class." ?
please explain.........

------------------

Help me!Help you!!!
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Well, it's a bit like forums, really. Say that Java in General (beginner) is the base class, and Java in General (intermediate) the subclass. Articles are like private methods, so that if you post a question in beginner (the base class), you cannot post the same question in intermediate (the subclass). If you do, people like myself will waste their time answering questions which have been perfectly adequately answered in another forum. Thank you.
- Peter
PS. Ditto for subsubclasses, of course, like Java in General (advanced). You really know how to illustrate a point, don't you?
william kane
Ranch Hand

Joined: Nov 21, 2000
Posts: 260
Thats what inheritance is all about right if u can provide a better implementation(answer) the user always has choice as the which implementation to use.......
I was not aware of that rule anyway..............
Fred Hosch
Author
Greenhorn

Joined: Apr 16, 2001
Posts: 23
First of all, since there is no overriding going on here, the
fact that bc refers to a DerivedClass instance at run time is a
red herring. it has no relevance at all. the static type of bc
is BaseClass.
Second, the lrm state that a protected member may be accessed from outside the package only by code "responsible for the implementation of the object." That means that dc.protectedMember() can appear in the definition of BaseClass
or of DerivedClass (where dc is declared to be of type DerivedClass), but bc.protectedMember() cannot appear in DerivedClass, UNLESS DerivedClass and BaseClass are in the same package.
is this a major KLUDGE? imho, yes it is.

Fred Hosch<br />Author of:<br /><a href="http://www.amazon.com/exec/obidos/ASIN/0471354899/ref=ase_electricporkchop/107-7882751-0234939" target="_blank" rel="nofollow">An Introduction to Software Construction with Java</a>
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
William, you might want to read our FAQ, which covers this and other issues. I'm closing this thread (and the one in Intermediate) to prevent addtional wasted effort by the various people who might reply.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inheritance thru upcasted reference