• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

inheritance thru upcasted reference

 
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.........

------------------
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Author
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To put it simply:
You can only run the protectedMethod() of BaseClass from inside of DerivedClass
Examples:
 
Fred Hosch
Author
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic