File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java 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 » Beginning Java
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
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Is this what you are trying to do? The error seems to indicate that you haven't actually used the extends keyword.Like my comment says, I'm not sure why you would want to instantiate the base class within the extended one, because if you instatiate the extended class (which you must do to use it), then you already have a base class instance.
[This message has been edited by Mike Curwen (edited April 18, 2001).]
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I've just looked at your topic. "Inheritance through upcasted reference". If you are trying to do what I think, you would need to use "containment & delegation" (which I would recommend against - strongly). Your 'derived classes' would contain an instance of the 'base class', and for every method in the 'base class', you must make a matching method in the 'derived class' that will then delegate the call to the contained instance. You can do this all without using the inheritance mechanism of Java (without the keyword extends)

I remember using containment and delegation while trying to "roll my own inheritance" in Visual Basic. It is messy and ugly and unsatisfying.
Notice that you get the same behaviour as inheritance, but with a lot more effort. Code like this is referred to as "boiler plate"
[This message has been edited by Mike Curwen (edited April 18, 2001).]
Fred Hosch
Author
Greenhorn

Joined: Apr 16, 2001
Posts: 23
Didn't i post this reply already?
there is no overriding going on here, so the fact that bc references a DerivedClass at run-time is a red herring. it is
completely irrelevent. the static type of bc is BaseClass.
now the lrm states that a protected member may be accessed from outside the package only by code "responsible for the implementation of the object." that means that the invocation
dc.protectedMethod() (where dc has static type DerivedClass)
can appear in the definition of BaseClass or inthe definition
of DerivedClass. But bc.protectedMethod() cannot appear in
the definiiton of DerivedClass, UNLESS DerivedClass and
BaseClass are in the same package.
a major KLUDGE? 'tis.

------------------
Fred Hosch
Author of:
An Introduction to Software Construction with Java


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>
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Fred,

I have a hard time understanding your post.

What do you mean by the "static type of bc is BaseClass" I don't see where it's being used from a static context, or am I misunderstanding?

Also, what is the 'lrm', and doesn't the last sentence contradict the one before it?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
To put it simply:
You can only run the protectedMethod() of BaseClass from inside of DerivedClass
Examples:


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Fred Hosch
Author
Greenhorn

Joined: Apr 16, 2001
Posts: 23
by "static" i mean "compile time type." (whoops! "static" has a
special syntactic meaning in java doesn't it?) that is, bc is
declared to be BaseClass; the compiler types each occurance of
the identified "bc" as "BaseClass." the fact that at run-time bc
actually refers to a DerivedClass instance matters only when
an overriden method is being invoked.
so as far as the compiler is concerned, you're trying to
invoke a protected method of a BaseClass from inside the
definition of DerivedClass. that's not allowed, unless
BaseClass and DerivedClass are in the same package.
the other way around is ok. if bc was declared to be
DerivedClass, and the invocation bc.protectedMethod() occured
in the definition of BaseClass, all would be fine.
"lrm" is "language reference manual" -- i.e., "The Java
Language Specification 2nd Edition."
sorry about the confusion :-0
------------------
Fred Hosch
Author of:
An Introduction to Software Construction with Java
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inheritance thru upcasted reference