Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

How to obtain the subclass name from superclass?

Thread view: 
www - 18 Oct 2007 13:46 GMT
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.
Thomas Fritsch - 18 Oct 2007 13:58 GMT
> 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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.