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 using a protected method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using a protected method" Watch "using a protected method" New topic
Author

using a protected method

Peter Straw
Ranch Hand

Joined: Jan 08, 2002
Posts: 79
Hi,
When I try to access the protected method removeRange() from the Vector class, I get a compilation error even though my class is a subclass of Vector. Why is this happening or am I doing smth wrong? ( I though it was possible to access protected members from a subclass)
Many thanks...
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Could you post the code and a copy of the compiler error message?


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Peter Straw
Ranch Hand

Joined: Jan 08, 2002
Posts: 79
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ August 19, 2002: Message edited by: Dirk Schreckmann ]
Peter Reinhardt
Ranch Hand

Joined: Aug 02, 2002
Posts: 57
You create a subclass, BUT you access the vector not via the subclass mechanism (which would be this.removeRange()) rather you access the vector instance through the normal method invocation (aVector.removeRange() ).
The vector you are acessing via aVector is not of type RemRange (i.e. you are not accessing your superclass).

Your class RemRange is implemented wrongly as well (therefor you get your problem, it is implemented as a wrapper around a vector and not as a subclass).

Peter.
[ August 19, 2002: Message edited by: Peter Reinhardt ]

SCJP 1.2, SCJD, SCEA, IBM 484, Weblogic 7
Peter Straw
Ranch Hand

Joined: Jan 08, 2002
Posts: 79
thanks Peter, that's fixed it
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
I think you are confused between "is a Vector" and "has a Vector". You are extending Vector but you also have a Vector as a member of your class. Do you really want to do both?
If you are extending Vector, you can call a protected method on this, but you can't call a protected method on a Vector that happens to be a member of this.
I don't know why Java chose to declare removeRange protected at all, since it seems to be a useful method. You might just want to declare your subclass to override removeRange with a public method that does the same thing:

[ August 19, 2002: Message edited by: Ron Newman ]
[ August 19, 2002: Message edited by: Jim Yingst ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using a protected method
 
Similar Threads
Doubt in protected modifier
why al can't access protected removeRange()?
protected variable of Vector
How the protected finalize method is called by garbage collector
problem in clone() while making singleton