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.........
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
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).]
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).]
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<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>
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