aspose file tools*
The moose likes Java in General and the fly likes Does the usage of static prevents us from OO Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Does the usage of static prevents us from OO" Watch "Does the usage of static prevents us from OO" New topic
Author

Does the usage of static prevents us from OO

Ravindranath Chowdary
Ranch Hand

Joined: Nov 08, 2006
Posts: 71
Hi friends,
I have read in one of the sites that, with the use of a static methods we will lose some of the behaviours of Object Oriented Programming. Open this url:

http://experts.about.com/q/Java-1046/static-methods-inheritance.htm

I wrote a code to check that. But inheritance concept is working well. Can you explain me why it is like this and in which case it happens like this.

Thanks,
Ravindranath.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
What do you expect to happen with these classes?

Let me know if it really works as expected.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ravindranath Chowdary
Ranch Hand

Joined: Nov 08, 2006
Posts: 71
Hi James,
Thanks for your explanation. In this method

private void testHonk( StaticSuper honker ) {
honker.honk();
}
Every time the honk of StaticSuper is only is called. How can we call the honk of SubClass with out changing the formal parameter of testHonk() to StaticSub. Is this the context, we can say with the use of Static methods we will lose the behaviour of OOPS.

Thanks,
Ravindranath.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Ravindranath Chowdary:
Is this the context, we can say with the use of Static methods we will lose the behaviour of OOPS.


Yes, that is Stan's point; static methods are not polymorphic.


[Jess in Action][AskingGoodQuestions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Yes, Ravindranath, nicely done. Statics using class name also tie the caller to the other class in a very rigid way. For example, when I first started Java I made a Logger with static methods because the syntax seemed very convenient:

Logger.log( message );

I used it in many little home projects. Now I'm stuck with it forever because I can't easily switch in another. If I hade made a Logger interface and a factory some of my classes would have had an extra line at the top:

// in member variables
Logger logger = LoggerFactory.getLogger();

// in some method
logger.log( message );

But with that I could use a different Logger implementation in any project by simply configuring the factory to return a different class.

So another way statics get in the way of Object Oriented Goodness is by very tight coupling.

BTW: I can redeem myself on the Logger. The static methods in my Logger can simply pass calls through to methods on any other Logger implementation. So how bad is the static method problem anyhow?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Does the usage of static prevents us from OO