Hello,
I have a question about inheritance and casting in Java. I have the following two example classes and a test class, and I state my question after the classes:
public class Automobile {
public int var;
public Automobile () {
var = 1;
}
public String toString () {
return "AUTOMOBILE: " + var;
}
}
public class Porsche extends Automobile {
public int var;
public Porsche () {
var = 2;
}
public String toString () {
return "PORSCHE: " + var;
}
}
public class Test {
public static void main (String [] args) {
Porsche p = new Porsche();
Automobile a = new Automobile();
System.out.println ("(Automobile) p = " + (Automobile)p);
System.out.println ("(Automobile) p.var = " + ((Automobile)p).var);
}
}
The output is:
(Automobile) p = PORSCHE: 2
(Automobile) p.var = 1
I don't understand why in the second statement we have 1. Shouldn't it be 2? Because after I cast p to Automobile in the first statement I still get PORSCHE: 2 as the representation of p - I understand this in the following way: nevertheless I have casted p to Automobile p keeps its "original nature" - p is an object of class Porsche, but because Porsche extends Automobile, we could say that p is also an Automobile. And therefore when we cast it explicitly to Automobile, it continues using its methods - in our case the method toString() defined in Porsche. On the other hand, if what I write is correct, then the second print statement should give 2 and not 1. Now this seems as a contradiction to me, but since this works in Java, it seems that I don't understand what's going on during the casting and the inheritance process. Therefore I am asking for a comment. Thanks you in advance.