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.
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();
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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. .