This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Overcome hiding method for static--pls help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Overcome hiding method for static--pls help" Watch "Overcome hiding method for static--pls help" New topic
Author

Overcome hiding method for static--pls help

Ann Sebastian
Ranch Hand

Joined: Sep 21, 2005
Posts: 37


Output::
The hide method in Animal.
The override method in Cat.

I want output to be
The hide method in Cat.
The override method in Cat.

Can this be done by retaining
1.static modifier for hide
2.Animal reference hold cat object

P.S. :This is just the replication of the problem I am facing in my project.
Please help.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Static methods cannot be overridden like non-static methods.

It's bad practice to call a static method on an instance of a class. Static methods are class methods, i.e. they don't operate on a specific instance of the class, so it's confusing if you call it on an instance of a class. Instead of calling a static method on an instance, you should call it on the class itself, i.e.: Cat.hide() instead of myCat.hide().

If this is a problem for you in your project, you need to re-think the design of your software.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ann Sebastian
Ranch Hand

Joined: Sep 21, 2005
Posts: 37
The problem is I have 3 subclasses of animal and I need to dynamically access the hide method of subclass.The hide method is static.I use Factory Design Pattern to dynamicaly generate the subclass.Is there any way to do this?
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
Do you really need hide method to be static ?


Regards,<br />Seb<br /> <br />SCJP 1.4
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Ann Sebastian:
The problem is I have 3 subclasses of animal and I need to dynamically access the hide method of subclass.The hide method is static.I use Factory Design Pattern to dynamicaly generate the subclass.Is there any way to do this?


It sounds like hide() should NOT be static then. I don't think it is possible to get the behavior you want with a static method. (Well, there is a way using the Reflection API, but that is probably not what you are looking for.)

Layne
[ October 11, 2005: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overcome hiding method for static--pls help
 
Similar Threads
Help on Inheritance
inplicit and explicit casting.
overriding
does static methods inherit ? what about private ?
static methods can�t be overridden