P
Paul J. Lucas
A given class Base has an nested class ParamBlock. Another class, Derived,
derived from Base also has a nested class ParamBlock. Derived's ParamBlock
is derived from Base's ParamBlock. In code:
class Base {
protected static class ParamBlock { /* ... */ }
protected Base( ParamBlock pb ) {
paramBlock_ = pb;
}
public ParamBlock param() {
return paramBlock_;
}
protected ParamBlock paramBlock_;
}
class Derived extends Base {
protected static class ParamBlock extends Base.ParamBlock {
int value;
protected ParamBlock( int v ) {
value = v;
}
}
Derived( int value ) {
super( new ParamBlock( value ) );
}
public ParamBlock param() { // illegal: wants Base.ParamBlock
return (ParamBlock)paramBlock_;
}
}
public class Test {
public static void main( String args[] ) {
Derived d = new Derived( 42 );
System.out.println( d.param().value );
}
}
I want Base's param() to return a reference to Base.ParamBlock and Derived's
param() to return a reference to Derived.ParamBlock. I am NOT looking for
polymorphism. If I call param() on an instance of a Base, I want
Base.ParamBlock; if I call param() on an instance of a Derived, I want
Derived.ParamBlock; that's it.
However, altering the return type of an inherited method in Java is illegal.
The equivalent code in C++ (below) is perfectly legal since param() isn't
virtual.
How can I get what I want in Java? I tried declaring param() final but that
didn't work.
- Paul
P.S.: Here is the equivalent (and legal) C++ code:
class Base {
protected:
class ParamBlock { /* ... */ };
Base( ParamBlock *pb ) : paramBlock_( pb ) {
}
ParamBlock *const paramBlock_;
public:
ParamBlock* param() const {
return paramBlock_;
}
};
class Derived : public Base {
protected:
class ParamBlock : public Base:aramBlock {
public:
int value;
ParamBlock( int v ) : value( v ) {
}
};
public:
Derived( int value ) : Base( new ParamBlock( value ) ) {
}
ParamBlock* param() const {
return static_cast<ParamBlock*>( paramBlock_ );
}
};
int main() {
Derived d( 42 );
int i = d.param()->value;
return 0;
}
derived from Base also has a nested class ParamBlock. Derived's ParamBlock
is derived from Base's ParamBlock. In code:
class Base {
protected static class ParamBlock { /* ... */ }
protected Base( ParamBlock pb ) {
paramBlock_ = pb;
}
public ParamBlock param() {
return paramBlock_;
}
protected ParamBlock paramBlock_;
}
class Derived extends Base {
protected static class ParamBlock extends Base.ParamBlock {
int value;
protected ParamBlock( int v ) {
value = v;
}
}
Derived( int value ) {
super( new ParamBlock( value ) );
}
public ParamBlock param() { // illegal: wants Base.ParamBlock
return (ParamBlock)paramBlock_;
}
}
public class Test {
public static void main( String args[] ) {
Derived d = new Derived( 42 );
System.out.println( d.param().value );
}
}
I want Base's param() to return a reference to Base.ParamBlock and Derived's
param() to return a reference to Derived.ParamBlock. I am NOT looking for
polymorphism. If I call param() on an instance of a Base, I want
Base.ParamBlock; if I call param() on an instance of a Derived, I want
Derived.ParamBlock; that's it.
However, altering the return type of an inherited method in Java is illegal.
The equivalent code in C++ (below) is perfectly legal since param() isn't
virtual.
How can I get what I want in Java? I tried declaring param() final but that
didn't work.
- Paul
P.S.: Here is the equivalent (and legal) C++ code:
class Base {
protected:
class ParamBlock { /* ... */ };
Base( ParamBlock *pb ) : paramBlock_( pb ) {
}
ParamBlock *const paramBlock_;
public:
ParamBlock* param() const {
return paramBlock_;
}
};
class Derived : public Base {
protected:
class ParamBlock : public Base:aramBlock {
public:
int value;
ParamBlock( int v ) : value( v ) {
}
};
public:
Derived( int value ) : Base( new ParamBlock( value ) ) {
}
ParamBlock* param() const {
return static_cast<ParamBlock*>( paramBlock_ );
}
};
int main() {
Derived d( 42 );
int i = d.param()->value;
return 0;
}