I don't understand a strange behavior of class inheritance.
This is my parent class:
public class Cubetti implements Token{
...
private int id=1;
...
public Cubetti(int n) {
numero = n;
}
...
public int getId() { return id; }
public void setId( int idx) { id = idx; }
...
}
and this is the subclass:
public class RGC extends Cubetti{
private int id=0;
...
public RGC (int idx) {
super(0);
id = idx; // STRANGE BEHAVIOUR !
}
...
}
This is the test mainclass:
public static void main(String[] args) {
RGC uno = new RGC(1);
RGC due = new RGC(2);
System.out.println(" uno Id is " + uno.getId() + " due Id is" + due.getId());
}
The output is
Uno Id is 1 and due Id is 1
but if I use in the tagged line on the RGC
subclass:
....
// id = idx;
setId(idx);
....
The output is
Uno Id is 1 and due Id is 2
Why?