M
marcwentink
To my surprise this seems possible in Java:
//-------------
public class Base {
protected int size = 100;
public int getSize() {
return size;
}
}
public class SubClass extends Base {
protected int size = 10;
public int getSize(){
return size;
}
public static void main(String[] args) {
Base b = new SubClass();
System.out.println (b.size + "," + b.getSize());
}
}
//--------------
Questions:
1. Why does the declaration of size not lead to a compile error in
SubClass, it seems to use a name already used.
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?
2. Is the Base version of size still reachable in the Subclass by
another statement? Something like Super()->size perhaps?
Furthermore, I know this example should show me that overridden
functions are subject of polymorphism, but vmember ariables are not.
The outcome of the program is: 100, 10.
//-------------
public class Base {
protected int size = 100;
public int getSize() {
return size;
}
}
public class SubClass extends Base {
protected int size = 10;
public int getSize(){
return size;
}
public static void main(String[] args) {
Base b = new SubClass();
System.out.println (b.size + "," + b.getSize());
}
}
//--------------
Questions:
1. Why does the declaration of size not lead to a compile error in
SubClass, it seems to use a name already used.
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?
2. Is the Base version of size still reachable in the Subclass by
another statement? Something like Super()->size perhaps?
Furthermore, I know this example should show me that overridden
functions are subject of polymorphism, but vmember ariables are not.
The outcome of the program is: 100, 10.