• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

static methods-overridden

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

Static methods can't be overridden but they can be redefined (from K&B)
class Animal {
static void doStuff() {
System.out.print("a ");
}
}
class Dog extends Animal {
static void dostuff() { // it's a redefinition, not an override
System.out.print("d ");
}
public static void main(String [] args) {
Animal [] a = {new Animal(), new Dog(), new Animal()};
for(int x = 0; x < a.length; x++)
a[x].doStuff(); // invoke the static method
}
}

in this how to recognise tht dostuff() is redefined??
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because it is declared static, it is hidden and not overridden.
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maddy,

Exactly I don't knwo what you really want to know???

But if you will run this class.

You will understand the logic.

Actually What happens in the static method.

The real thing which matters is Class Reference, although you have overrided the method but you are calling method still with super class reference. that's why it is calling super class method.

That's it.


 
mad dy
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

when I compi led and run this program i got o/p "a a a"

wht my question exactly is by seeing the code it looks like overriding the method dostuff in subclass Dog...
but actually it is redefining it..
I'm not able to understand the difference b/w them
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will see the difference if you remove the keyword static from the definitions.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mad dy
For your example the answer is correct.
The reason behind is STATIC methods cannot be overridden
but we can achive this.When we compile the program
with static methods the compiler resolves at compile time
that is it calls the super class static method.
so when you try to execute the method on
Super class reference variable using subclass object .
The only super class static method execute.
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More Stuffs
 
mad dy
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks All,

Now I got the clear Idea.

As the static method of Super class is hidden, so it is not inherited to the Sub Class.
So there is no issue of overridding the super class static method in Sub class,
and the sub class will treat its static method as it's own method...

Am I Right???
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not overiding since static methods only exist in the class they are defined in. The fact a subclass has a static method with the same name is pretty much irrelevant.

When coding it is recommended to refer to static methods by their class and not via an instance variable (e.g MyClass.doStaticStuff() and not myIntance.doStaticStuff()), which would clear up any confusion about which static method will be called. In the exam if you just look at the type the reference is referring to and not what class initialised it, you shouldn't have any confusion.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't the dostuff in the Dog class completely different method because of the lowercase s ???
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Revathi you are right...

Its not redefinition, rather its a altogether new method.

I think he has done typo mistake.

If you use this line...

a[x].dostuff(); // invoke the static method

instead of

a[x].doStuff(); // invoke the static method

It won't compile. Compiler will complain dostuff method not defined in Animal class. Remember java is case sensitive.


Regards


Naseem
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is possible to access static method and variable using class object of this method and classs OR just using Class Name...
 
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
^^Hello all,

This is my first post.

Yes, you can call a static method or class variable using the instance of the class where these static method or class variable are defined.

However, we cannot call a non-static method or class variable using a class name.

Please share your inputs on these if you have one.
 
when your children are suffering from your punishment, tell your them it will help them write good poetry when they are older. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic