So why the class property has not changed?
Since the object not changed, just the type of link you have. Casting does not affect the object itself.
In Java, unlike some other languages (fortunately), the type of link does not significantly affect the version of the version you receive. For example, consider these two classes (kindly 2rs2ts - thanks!):
class Base { public Base() {} public void foo() { System.out.println("I'm the base!"); } } class Child extends Base { public Child() {} public void foo() { System.out.println("I'm the child!"); } }
This code:
Child x = new Child(); Base y = (Base) x; y.foo();
... exits
I'm the child!
because although the y type is Base , the object we call foo on is Child , and therefore Child#foo is called. Here (again kindly provided by 2rs2ts ) this is an example on ideone to play with.
The fact that we get Child#foo , despite looking at the Base link, is crucial for polymorphism.
Now it so happens that the method you called ( getClass ) can only be Object#getClass , because it is the final method (subclasses cannot override it). But the concept is crucial, and I thought it was probably the core of what you asked for.
The main thing that the type of link does is determine which aspects of the object you are allowed to. For example, suppose we add bar to Child :
class Child extends Base { public Child() {} public void foo() { System.out.println("I'm the child!"); } public void bar() { System.out.println("I'm Child#bar"); } }
This code will not compile:
Child x = new Child(); Base y = (Base) x; y.bar();
... because Base does not have a bar method, so we cannot access the bar object by a method through a reference of type Base .