Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using a protected method

 
Peter Straw
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post the code and a copy of the compiler error message?
 
Peter Straw
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ August 19, 2002: Message edited by: Dirk Schreckmann ]
 
Peter Reinhardt
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Peter Straw
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Peter, that's fixed it
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic