• Post Reply Bookmark Topic Watch Topic
  • New Topic

Ovveriding Static Method.  RSS feed

 
Shahir Deo
Ranch Hand
Posts: 80
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi every One , I know we cant ovveride the static method, but i did not find any strong reason for that , we can eliminate duplicacy angle by putting static methods in different classes but question remain same. why static methods cannot be ovverided?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Think about how overriding works. The compiler only looks at the type of the reference to determine which signature we're calling and to see if the reference type has a method with that signature. Then at runtime, the JVM looks at the object itself to see what class it is and what that class's version of the method is.

So for #1 and #4, the compiler determines that Parent has m1(), so it's legal. Then at runtime, for #1, the JVM see's that the object is of class Parent, and invokes Parent's m1(), and at #4 it sees that the object is of class Child and so it invokes Child's m1().

But for static methods, there is no associated object. Even when we use a reference like at #2 and #5--instead of the preferred form of using the class name as in #3 and #6--the JVM never looks at any objects, because no object is needed to call a static method. In fact, we could set the references to null, as in #7, and call the static methods, and there will be no NPE.

Of course, it could be asked, "But why didn't they design the language so that if there is a reference instead of the class name, it follows it to the object and calls the overridden method, just like with the non-static case?" Sure, they could have done that, and there are languages (Smalltalk I think, and maybe C++, probably others) where static methods can be overridden.

But the designers chose not to do so in Java. Unless we ask them or get hold of their meeting notes and white papers, we can only speculate as to why they made that decision. A reasonable guess, however, is that they felt it would add too much complexity to the language for too little value. Simplicity has always been one of the core goals of Java.
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The whole concept of overriding and polymorphism depends on class instances.
To work with polymorphism and then on overriding you need to have instances.

So, it doesn't works with static methods as they do not belong to any particular instance; they belong to the whole class.

cheers :-)
 
Shahir Deo
Ranch Hand
Posts: 80
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation , both of you.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lalit Mehra wrote:The whole concept of overriding and polymorphism depends on class instances.
To work with polymorphism and then on overriding you need to have instances.


In general no, but in Java in particular, yes.
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:

In general no, but in Java in particular, yes.


I think the question was asked in the Java Section Itself ...
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!