• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

static methods

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
chapter 2 of K&B book(page no-151)
It says that the static methods cannot be overridden but they can be redefined in a subclass.
i am not able to understand as to how redefining is different from overriding.
the following code is from the K&B book
class animal
{
static void doStuff()
{
System.out.println("A");
}
}
class dog extends animal
{
static void doStuff()
{
System.out.println("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();
}
}
}


the book says that the doStuff() method is redefined in class dog.....i feel that the doStuff() method is being overridden....please clear my doubt
thank you in advance.



 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lets say if you declare
animal a = new dog();
a.doStuff() is equal to animal.doStuff();
the jvm will never look at declare type in case of static method.
that's why polymorphism does not apply on static methods.

hth.
 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vasundhra please Use Code Tags when you post a source code. That way your code looks formatted. Unformatted code is hard to read. You can add code tags by wrapping your code in [code] [/code] tags. You can edit your message using button and then add code tags to it...
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at this example:


Output is:
B.a
A.b
B.a
B.b

A type of variable 'a' is A ... but concrete object is of type B, so a call to overridden method a() executes a method from concrete object of type B,
not a method from class A.
In case of static method b() this works in a different way - the method b() from class A is called (A is declared type of variable a).

I hope this will help.
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To me words 'overriden' and 'redefined' in one sentence were too much. Redefined is like code writen again, while 'overriding' has allready some JVM concept stuff. And 'overriding' could be as well 'redefined'. And when said 'redefined', then why it shouldn't 'redefine' so much, that it's like overriding, when it's redefined.

Anyway, this is not an explanation but diving into a confusment. I like to do that, from some strange reasons. Allthough I get point, becaouse i understand, and know a bit, and all, I just like to stay on that "first thought", "first association". Has nothing to do with programming, actually.

Maybe it's said that it's redefined becaouse it has nothing to do with a superclass, like theoretically. But it isn't so. If there wouldn't be a "same" method , so, like if it would be "empty", then it would be inherited, so it would be there, in a subclass.

But if you write it, it isn't there, becaouse you wrote yours.

Which sounds honestly like overriding. But it isn't. The 'mechanism' which might be imagined there, something that will happen, doesn't exist, so there is no further steps, nothing happens, it stays on that place. It overwaits. Overriding is like an 'alive object' in an 'alive program' thing. (But you redefine. Here it get's funny for me.)

Just when you call the stuff, it doesn't really care about the instance, what's an 'alive' object in memory. (Static methods, no? Aka. Class Methods thing)

Like... it's not even advisible to call a static method on a reference variable, object, a.static(), but on a clas, A.static().

I know that this what I'm saying absolutely falls out of logic. That's why it is funny to me.


But why the thing doesn't work with the instance variables?
 
vasundhra sharma
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much ...........
i finally have the concept registered in my mind......
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can some please help me in understanding the output of the example give by "Ireneusz Kordal"...

Thanks and Regards,

Arun Yadav
 
Petar Thomas
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
: ))) I can. : )))))) The same thing. It's easy, just that it's written on that way. Could be written differentlly, but this is , like , 'reduced', 'reduction'.

One part which might be confusing, is that what goes out on a consola, with a System.out.println, is a text similar, and simple as well, like the name of the classes and the name of the methods, which are void, or static, the other part is that all is called A A b B a b ... So, when the "trick" happens, it could be even more mysteriouse.

But practically, it's about counting, like counting cards.

So, in a main method, that's like this part of the car which starts when you click the key (i don't know nothing about cars)

there are four 'CALLS' , one after another

(that's important, that they are one after another)

and on the screen, there is a .. four outputs.


- As it can be observed, in the whole program, there is, here , nothing more, but printing out to the screen.

And every method has only one printout.

So, what is needed, is to see which method printed what.

There is: FOUR CALLS

and : FOUR OUTPUTS


The Question IS: Which 'CALL', in a source code, corresponeds to an actuall method??


Four - out - of - four . Pair the calls and methods!



ANd, then there ther is, void methods are ectually (default) voids... and might be overriden, and static void methods, are actually (default) static methods, which arent overriden, but redefined.

To my eyes it was missing public all around instead of (default), i consider it as a part of the "trick".

And this was, by my opinion, a nice code to confuse youre self, and then resolve confusement. But when you 'reduce' the 'reduced' version, you should see that overriden methods don't behave the same like redefined methods, all in context of polymorphism and I don't know what, becaouse static methods are called diferentlly than instance methods. In instance methods, when in source code stays that there is a 'CALL', during the 'exectution time', when frankenstein lives, which method will really be called, is chosing a JVM..... by some rules. So, if there is a static method what's this about... then the -> type of the reference variable ______ like Animal a = new Dog(), Animal static method will be called....... While, if it's about an ordinary instance method... then... if Dog overriden the same Animal method, althou it is a Animal reference variable a, the actual 'real' object is Dog, and if Dog overriden, then the Dog method is called, instance method... Without it, it might be that the instance thing would be questionable.. So the question was.... Why.. if a subclass 'overrides' a superclasses STATIC method, and 'overriden' version isn't called?? (More or less).. Becaouse it is not 'overriden', but it is 'redefined'...

And code shows which is which.

Now you choose a card, and I'll tell you which one you chosed.
 
Petar Thomas
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

M M kaimkhani wrote:lets say if you declare
animal a = new dog();
a.doStuff() is equal to animal.doStuff();
the jvm will never look at declare type in case of static method.
that's why polymorphism does not apply on static methods.

hth.



Aha. And a litlle notice.
This is also what I was been saying.

But there is a , a 'small flaw in logic of it', i think, at least at what I was sayin, why it is a bit dangereouse to say it like that.

Becaouse, what i think, is that "there are TWO THINGS"
1) this thing - a.a and A.a, or.. a.doStuff() and animal.doStuff()..
2) and the thing (ahaa, i got a clear idea)... anyway, the thing about calling, when the calling happens, or happenings..(not realy like that)

One thing is a compile time thing (or typewritting)

The other thing is a EXECUTION time thing.

It is kind of dangeriouse.. to.. to.. mix it. So , I think that, it's an all right explanation, but this small discrepancy, should stay in a sight.

 
binnu yadav
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many thanks Petar for this wonderful reply..........thanks a lot....
 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case of Static methods polymorphism does not work. In the code “a” is of type A and is initialized with class b (A a = new B()). So if we call an ordinary method, JVM will call the method of class B, but in case of static method JVM will call the static method of Class A (actual variable type) hence the output is “B.a and A.b”.

Same is the case with B b = new B(). Because actual variable and reference is same so methods of class B will be called (Here polymorphism will not work because actual and reference type are same.
 
Petar Thomas
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You a lot for saying it on suchaway... it makes me feel... wonderfull.. actually wonderfull, and a bit more else..

I send you a song, and tho everyone.

http://www.youtube.com/watch?v=JMQdLE2jbFg


Steve Harley & Cockney Rebel: Sebasitan

and lyrics
http://www.lyricsfreak.com/s/steve+harley/sebastian_20130799.html


...a pieace out of lyrics> "... love's a story we'll serialize "
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Polymorphic method invocations apply only to instance methods, Not static methods, Not variables.
Only overridden instance methods are dynamically invoked based on the real object's type.

for example


Here reference is of type A, but at runtime method of actual object will run, means method b() of class B will run. But in the case of static methods and variables method of reference type will run.
 
My, my, aren't you a big fella. Here, have a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic