> To my surprise this seems possible in Java:
>
[quoted text clipped - 17 lines]
> }
> //--------------
IMHO this is bad design anyway. Rather make the member private and
accessible via a getter only. If you want to prevent overriding you can
make getter and setter final.
> Questions:
>
> 1. Why does the declaration of size not lead to a compile error in
> SubClass, it seems to use a name already used.
It's in a different scope and depending on the compiler you use you
might be able to get him give you a warning about a hidden member.
> But I could accept that since in an inner loop you can also declare a
> variable with the same name as in the outer loop. I probably can
> compare it to that, right?
Can you? I get an error for this
for ( int i = 0; i < args.length; ++i ) {
for ( int i = 0; i < args.length; ++i ) {
System.out.println( i );
}
}
> 2. Is the Base version of size still reachable in the Subclass by
> another statement? Something like Super()->size perhaps?
public class SubClass extends Base {
protected int size = 10;
public int getSize() {
return size;
}
public int getSuperSize() {
return super.size;
}
public static void main( String[] args ) {
SubClass b = new SubClass();
System.out.println( b.size + "," + b.getSize() );
System.out.println( b.getSuperSize() );
}
}
> Furthermore, I know this example should show me that overridden
> functions are subject of polymorphism, but vmember ariables are not.
What exactly do you want to say with this?
> The outcome of the program is: 100, 10.
Right.
Kind regards
robert
marcwentink@hotmail.com - 24 Aug 2006 16:57 GMT
Robert Klemme schreef:
> IMHO this is bad design anyway.
Agreed, it is just an exercise example.
> Can you? I get an error for this
> for ( int i = 0; i < args.length; ++i ) {
> for ( int i = 0; i < args.length; ++i ) {
> System.out.println( i );
> }
> }
I will try that. Perhaps this only goes for C++ then.
> public int getSuperSize() {
> return super.size;
> }
Aha!
> What exactly do you want to say with this?
Nothing much, just that my question is not about what the example
program tried to explain, I got that.
> Kind regards
Hey thanks a lot!