Undestanding Java Inheritance: strange behaviour -
I do not understand class behavior as a strange behavior.
This is my parental class:
Public class cubettes applied token {... private id = 1; ... public cube (int n) {numero = n; } ... public int getId () {return id; } Public Zero setId (int idx) {id = idx; } ...} and this is the subclass:
Public class provides the RGC Cubetry {Private id = 0; ... public RGC (Int IDX) {super (0); Id = idx; // strange Behaviour ! } ...} This test is the main category:
public static zero main (string [] args) {RGC ano = new RGC (1 ); RGC Reason = New RGC (2); System.out.println ("anonymous id" + uno.getId () + "pay id" + reason .getId ()); } is output
Uno ID is 1 and the reason is ID1
but if I use these I have the line Tagged on RGC subclass:
... id = idx; SetId (IDX); .... The output is
the Uno ID is 1 and the reason is ID2
Why?
You have id variable both Cubetti super -class and RGC sub-orbit setter and Greater Update use / super-class id , because those methods are defined in the superclass and the sub -class is not overridden by sub-squared variable modifying the sub-class variable in calling id = idx , because sub-squares variables are super-class variables If it 'hide it, you will not be able to access it from the sub-class, because it is private, and even if it is private.
Comments
Post a Comment