jQuery in Action, 2nd edition
The moose likes Java in General and the fly likes overriding static method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "overriding static method " Watch "overriding static method " New topic
Author

overriding static method

saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
hi refering to this article
http://faq.javaranch.com/view?OverridingVsHiding




in the LineX Foo f = new Bar();

the type is Base class
if i create Bar f=new Bar();
then the outputs is like this
instanceMethod() in Bar
classMethod() in Bar

beacuse we genrally write the code in this way not like lineX code

so with this i can say that static methods can also be overriden

can any one suggest me the better way ... that we cannot override static methods

thanx in advance


A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

You say

beacuse we genrally write the code in this way not like lineX code


But in fact, the whole concept of method overriding -- i.e., polymorphism -- depend on writing code equivalent to "lineX". Let's look at the toString() method, for example. When you override toString() for a class, then when you print an object, toString() is called and the result is displayed. How does this work? PrintStream has a method

public void println(Object o)

When you call println() on an instance of your class Foo, it is this method that is called. And this method (through an intermediary) then calls

o.toString();

Note that we're calling toString() on a variable of type Object, but we want Foo's toString() method to be called. This is exactly like your "lineX", in which you have a reference of type Foo, but would like Bar.classMethod() to be called -- but it doesn't work. That's because static methods aren't polymorphic. The compiler chooses which method to call when the code is compiled based on the type of the reference (Foo), not at runtime based on the type of the instance (Bar).

So you see that the concept of method overriding simply doesn't exist for static methods -- instead, you have the ability to define a method by the same name with the same signature in a subclass, but without polymorphic behavior.


[Jess in Action][AskingGoodQuestions]
saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
So you see that the concept of method overriding simply doesn't exist for static methods -- instead, you have the ability to define a method by the same name with the same signature in a subclass, but without polymorphic behavior.[/QB]


sir, i think u havn't got my point

in my previous code
i mean in the lineX code i am taking the reference type as base class and creating the instance of child class

but we generally never do this :roll:
because if we want to create an instance of child class we just create the reference type as child class and we create an object for child class
exactly like this

Bar b =new Bar();
and we will call the child class methods
f.instanceMethod();
f.classMethod();

but we never get an idea to create base class reference variable holding child class obj , i mean exactly like this
Foo f=new Bar();
f.instanceMethod();
f.classMethod();


so now justify yourself ....







Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14672
    
  11

but we generally never do this

I do. People using Spring do it all the time.
Have you ever heard of "programming to interfaces" ?

When you use a new ArrayList, what are you doing ?
1. ArrayList arr = new ArrayList();
Or 2. List arr = new ArrayList(); ?


[My Blog]
All roads lead to JavaRanch
saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
Originally posted by Satou kurinosuke:

I do. People using Spring do it all the time.
Have you ever heard of "programming to interfaces" ?

When you use a new ArrayList, what are you doing ?
1. ArrayList arr = new ArrayList();
Or 2. List arr = new ArrayList(); ?



i do the first approach
ArrayList arr=new ArrayList();
:roll:
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by saikrishna cinux:

but we generally never do this :roll:


What do you mean "we", Kemosabe?
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
Originally posted by saikrishna cinux:

i do the first approach
ArrayList arr=new ArrayList();
:roll:


Which is a very bad approach.
You should do List list = new ArrayList(); or even Collection col = new ArrayList(); instead (depending on whether you will need the actual methods in List that don't exist in Collection).

Makes it a lot easier to change your system to another Collection implementation if needed.


42
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
Saikrishna , if you want to have a useful discussion here, you would probably be better off not using :roll: so much when discussing things you don't understand.


"I'm not back." - Bill Harding, Twister
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
The only reason this is a problem is because you're calling the method on an instance of the class, when you should be calling it on the class itself:If you do that, you'll never be in doubt about which method you're calling (as mentioned in the FAQ article). I never call class methods on instances.

By the way, I hope everyone realizes that all this talk about programming to interfaces is irrelevant to the original question. Since you can't declare static methods in interfaces, the issue could never arise.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by saikrishna cinux:

in the LineX Foo f = new Bar();

the type is Base class
if i create Bar f=new Bar();
then the outputs is like this
instanceMethod() in Bar
classMethod() in Bar


True.

Originally posted by saikrishna cinux:
beacuse we genrally write the code in this way not like lineX code


No, "we" do not. You will find very few people that do this and most of them are inexperienced programmers who don't know any better yet.

Originally posted by saikrishna cinux:
so with this i can say that static methods can also be overriden


You can call it green tea if you like but that doesn't make it so. Your example does not show a method being overridden, it shows two methods with the same name in two completely different classes. One does not override the other and they cannot be polymorphically called.

Originally posted by saikrishna cinux:
can any one suggest me the better way ... that we cannot override static methods

thanx in advance


Static methods belong to the class, not the instance. There is no polymorphism and they simply cannot be overridden. If I declare method m in class Foo and method m in class Bar then one method belongs to Foo and one to Bar. Whether or not Bar extends Foo or vice versa is irrelevent, they are still two distinct classes and the method belongs to the class, not to any instance of the class.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Alan Moore:
The only reason this is a problem is because you're calling the method on an instance of the class, when you should be calling it on the class itself:If you do that, you'll never be in doubt about which method you're calling (as mentioned in the FAQ article). I never call class methods on instances.

By the way, I hope everyone realizes that all this talk about programming to interfaces is irrelevant to the original question. Since you can't declare static methods in interfaces, the issue could never arise.


It's entirely relevent as their argument is predicated upon this presumption that "we" always declare a variable as it's implementation. Programming to interfaces was brought up in an attempt to explain to them why this is not only inaccurate (in the sense that they act as if most do this) and undesirable (in the many ways it is a bad practice).

Saikrishna, you really ought to reconsider your approach. Acting condescending when talking about things you clearly don't understand isn't a good way of getting answers out of people. Ernest did justify himself, you have not. You have not demonstrated in any shape or form a static method being overridden because you apparently don't understand the concept of polymorphism and what overriding actually means. .
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: overriding static method
 
Similar Threads
Why it is illegal to use abstract and static modifiers together in method declaration ?
static methods?
Polymorphism and Static methods
static method
Method overriding and hiding problem.