There is a huge difference between variables and methods: methods can be overridden, variables can't (although it looks like it, but a variable in a subclass is just shadowing a variable with the exact same name in the parent class.
That's why in your example
((A)b).x will print 15 (no overriding, A and B just have a variable with the same name), but with method doStuff you have overriding and thus polymorphism. If you have an instance of type B, it will always run the doStuff-method of class B. Casting to A won't help, because which (overridden) method to execute is decided at runtime and at runtime your instance is still from class B (so its method is called).
You can however (like illustrated by Mala) call the method of the parent class by calling super from the method from the child class. Or you can create an instance of class A and invoke its doStuff-method. But creating an
George Panadis wrote:I am trying to call A.doStuff() instead of B.doStuff() by using a B object in another class (such as C).
That's simply impossible!