Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Does the usage of static prevents us from OO

 
Ravindranath Chowdary
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you expect to happen with these classes?

Let me know if it really works as expected.
 
Ravindranath Chowdary
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic