Re: How to call an overridden method?



Java doesn't have the ability (AFAIK) to have non-virtual methods.
So...
A a = new B();
B b = new B();
a.f();
b.f();

Both print "B"

In C++ you have to explictly say these are "virtual" to get that
behavior.
In Java it is implied and that's that.
No casting can get around the fact that methods are virtual.

Possible work arounds (sorta).
1) Add the "final" to A's f() to prevent any subclass from overridding
it.

2) Add a new method to B called superF() or something that just calls
super.f().
In other words just give them different names.

There may be a way to do this using the reflection API but I haven't
seen it and I don't think its likely.

Class pSuperClass = b.getClass().getSuperclass();
A realA = pSuperClass.newInstance();
realA.f();

But thats insane and the exactly the same as just writing:
A realA = new A();
realA.f();
.