• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why Do I Need Protected instead of Private?  RSS feed

 
Kevin Simonson
Ranch Hand
Posts: 223
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I implemented a Red Black Tree for a program I'm writing like so:

and then I wrote a driver to test it:

which works just fine. But I had to cast the result of method {insert()} to get an object of type
{Frwrd}, so I thought I'd use Java generics, and pass {Frwrd} in as a parameterized type. So
rewritten versions of the same two files are:

and driver:

When I compile this I get:

D:\Langs\Java\Src\Alan\Alt>\Langs\Java\8u40-windows-x64\bin\javac -Xlint:unchecked RedBlack.java
RedBlack.java:47: warning: [unchecked] unchecked cast
    newRoot.high.set( inq, (TR) this);
                                ^
  required: TR
  found:    RedBlack<TR>
  where TR is a type-variable:
    TR extends RedBlack<TR> declared in class RedBlack
RedBlack.java:59: warning: [unchecked] unchecked cast
      return (TR) this;
                  ^
  required: TR
  found:    RedBlack<TR>
  where TR is a type-variable:
    TR extends RedBlack<TR> declared in class RedBlack
RedBlack.java:63: warning: [unchecked] unchecked cast
    { stRoot.merge( (TR) this);
                         ^
  required: TR
  found:    RedBlack<TR>
  where TR is a type-variable:
    TR extends RedBlack<TR> declared in class RedBlack
RedBlack.java:72: error: rotate(int) has private access in RedBlack
        { stRoot = stRoot.rotate( 0);
                         ^
  where TR is a type-variable:
    TR extends RedBlack<TR> declared in class RedBlack
RedBlack.java:75: error: rotate(int) has private access in RedBlack
        { stRoot = stRoot.rotate( 1);
                         ^
  where TR is a type-variable:
    TR extends RedBlack<TR> declared in class RedBlack
2 errors
3 warnings

D:\Langs\Java\Src\Alan\Alt>

The announced errors on lines 72 and 79 can both be fixed by replacing the {private} qualifier for
method {rotate()} with {protected}, and then the new version of {Frwrd} works just fine. But why
does it need to be {protected}? Method {stInsert()} is declared to be {private}, and that works just
fine when called by method {insert()}; why can't method {insert()} be happy when it calls {private}
method {rotate()}?

The other things the compiler complains about don't keep the program from compiling, but they're
still of some concern to me. They entirely have to do with the {(TR)} cast on {this}. That being the
case, I don't know why the compiler is complaining; {TR} is declared to be a subtype of {RedBlack},
so clearly a cast from {RedBlack< TR>} to {TR} is legal. So why the compiler complaint?

Is there some other way to implement a Red Black Tree that wouldn't involve code that would generate
these types of complaints? Or should I just be happy with the fact that the code compiles?
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As for your casts: sure, they are legal but they might lead to ClassCastException at run time.

As for your question about



... in your original non-generic code the types were RedBlack, so why isn't this



?

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!