• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

overriding static method

 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ....







 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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(); ?
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by saikrishna cinux:

but we generally never do this :roll:



What do you mean "we", Kemosabe?
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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. .
 
That is a really big piece of pie for such a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic