How to obtain the subclass name from superclass?

W

www

Hi,

I have three classes(Animal, Cat and Dog).

public class Animal
{
//...

public void printOut()
{
System.out.println( + " is cute!"); //un-finished. My questions is below
}
}

public class Cat extends Animal
{
//...
}

public class Dog extends Animal
{
//...
}

I want to achieve the following:

Cat myCat = new Cat();
myCat.printOut(); //"Cat is cute!"

Dog myDog = new Dog();
myDog.printOut(); //"Dog is cute!"

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.

Thank you very much.
 
T

Thomas Fritsch

www said:
I have three classes(Animal, Cat and Dog).

public class Animal
{
//...

public void printOut()
{
System.out.println( + " is cute!"); //un-finished. My questions is below
}
}

public class Cat extends Animal
{
//...
}

public class Dog extends Animal
{
//...
}

I want to achieve the following:

Cat myCat = new Cat();
myCat.printOut(); //"Cat is cute!"

Dog myDog = new Dog();
myDog.printOut(); //"Dog is cute!"

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!");
}
 
L

Lew

www said:
I want to achieve the following:

Cat myCat = new Cat();
myCat.printOut(); //"Cat is cute!"

Dog myDog = new Dog();
myDog.printOut(); //"Dog is cute!"

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()?

Thomas said:
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.
 
W

www

Thomas said:
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.
 
T

Thomas Fritsch

Lew said:
Note that this depends on the override of getClass().

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.
 
L

Lew

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.
 
L

Lew

Thomas said:
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.

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.
still holds.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top