> I have three classes(Animal, Cat and Dog).
>
[quoted text clipped - 28 lines]
> Is this possible? Only one printOut() in all three classes, and that is
> in superclass Animal. I don't want to over-write printOut() in Cat or Dog.
Write this method in class Animal:
public void printOut()
{
System.out.println(getClass().getName() + " is cute!");
}

Signature
Thomas
Lew - 18 Oct 2007 14:04 GMT
www wrote:
>> I want to achieve the following:
>>
[quoted text clipped - 6 lines]
>> Is this possible? Only one printOut() in all three classes, and that is
>> in superclass Animal. I don't want to over-write [sic] printOut() in Cat or Dog.
The word is "override".
Why do you not want to override printOut()?
> Write this method in class Animal:
> public void printOut()
> {
> System.out.println(getClass().getName() + " is cute!");
> }
Note that this depends on the override of getClass().
Somewhere there will be an override.

Signature
Lew
Thomas Fritsch - 18 Oct 2007 14:42 GMT
>> Write this method in class Animal:
>> public void printOut()
[quoted text clipped - 5 lines]
>
> Somewhere there will be an override.
Eeeh... no!
The declaration in class java.lang.Object is
public final native Class getClass();
Hence, because method getClass() is final it cannot be overridden.
The fact that getClass() returns different class objects (Dog.class,
Cat.class or whatever) is achieved by the native code, not by any
overriding.

Signature
Thomas
Lew - 18 Oct 2007 14:56 GMT
>>> Write this method in class Animal:
>>> public void printOut()
[quoted text clipped - 12 lines]
> Cat.class or whatever) is achieved by the native code, not by any
> overriding.
Well, shiver me timbers!
The native code acts like the helper methods I described elsewhere in this
thread, so while getClass() itself is final, the logic that it calls in turn
is non-final. Just take my argument and push it back to the point where the
class behavior is actually overridden.
>> Somewhere there will be an override.
still holds.

Signature
Lew
www - 18 Oct 2007 14:32 GMT
> Write this method in class Animal:
> public void printOut()
> {
> System.out.println(getClass().getName() + " is cute!");
> }
Thank you. It works great in my program.
Sorry for the wrong term "over-write". It should be "override" really.
Lew - 18 Oct 2007 14:45 GMT
Thomas Fritsch wrote:
>> Write this method in class Animal:
>> public void printOut()
>> {
>> System.out.println(getClass().getName() + " is cute!");
>> }
> Thank you. It works great in my program.
>
> ... override ...
Well, you just needed a to use a different override. Thomas showed you how to
use the override of getClass() to do what you wanted.
The technique of having a superclass method invoke another, overridden method
is frequently used. In this case, printOut() is not being overridden (and
should therefore be marked 'final'), but delegates subclass-specific action to
getClass().
A data access object (DAO)-layer superclass might do something similar. A
final superclass "retrieve()" might delegate to a subclass "getSql()" to
populate a PreparedStatement. The retrieve pattern will be more or less the
same for all subclasses: getSql(), substitute parameters, executeQuery(),
transfer results to a non-JDBC collection. The final superclass method will
direct all the overridable (possibly even abstract) methods in the correct
sequence; each overridden helper method like getSql() will do the right thing
for the subclass.

Signature
Lew