1

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?

Eran
  • 387,369
  • 54
  • 702
  • 768
Ido Traini
  • 83
  • 1
  • 10

2 Answers2

3

You have an id variable in both the Cubetti super-class and RGC sub-class. Using the setter and getter updates/returns the id of the super-class, since those methods are defined in the super-class and are not overridden by the sub-class.

Calling id = idx in the sub-class constructor modifies the sub-class's variable, since the sub-class's variable hides the super-class's variable, and even if it didn't hide it, you wouldn't be able to access it from the sub-class, since it's private.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Ok. Probably i have to improve my concept of inheritance. I'm an old c programmer and i'm learning programming from zero again after some decades.. so be patient with me :) Correct me if i'm in wrong: since now i've though a subclass as something that take a model from the parent class but have a sort of "formal" indipendence. Instead i have to think a subclass instance as formally a parent class instance with something more, so when i define a subclass behavior i have some stricts constraints, i can add some funcionality, but not a lot more – Ido Traini Nov 04 '14 at 12:57
  • @IdoTraini A subclass may add state (variable) and behavior (methods) to those of the parent class. It can also override behavior (methods) of the parent class (assuming they are not private or package-private for a parent class located in a different package). It can't, however, override variables of the parent class. If it defines variables of the same name as in the parent class, it hides them. – Eran Nov 04 '14 at 13:03
0

In Java::
private properties are not inherited to the derived class. So the Cubetti's private property is not seen (directly accessible) in the scope of RGC's class. Cubetti's id property is only accessible through the outside world using the get() & set() methods (if these method are declared public).

In the 1st case where your output is following:
Uno Id is 1 and due Id is 1
The reason is that, the Cubetti's getId() & setId() methods are inherited to the RGC class as they are public method of Cubetti class. So whenever you call the get() method you get the value of the Cubetti's id value, which is set to 1. That is why you get the value 1 for both Uno & Due.

In the second case:
you place setId() in the RGC constructor. Here you basically set the id of the Cubetti's ID. Thus calling the getId() the id of the Cubetti is being returned.

Ratul Sharker
  • 7,484
  • 4
  • 35
  • 44