Re: How to call an overridden method?
- From: "David A. Re***" <tinyweldingtorch@xxxxxxxxx>
- Date: Fri, 30 May 2008 06:27:58 -0700 (PDT)
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();
.
- Follow-Ups:
- Re: How to call an overridden method?
- From: Peter Duniho
- Re: How to call an overridden method?
- References:
- How to call an overridden method?
- From: Aaron Fude
- How to call an overridden method?
- Prev by Date: Re: Trash Can and Sun
- Next by Date: Re: WORA high performance
- Previous by thread: Re: How to call an overridden method?
- Next by thread: Re: How to call an overridden method?
- Index(es):