• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static methods

 
Suresh KumarPandey
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SCJP by Sierra and Bates say that static methods can't be overridden but they can be redefined.can someone please explain using an example.i couldn't understand.
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Static methods cannot be overridden because they simply would not be visible to derived classes (sub-classes).
If you define a method with the same signature in a sub-class you would be declaring a new/different method.
This is what K&B mean by "redefining".

 
Suresh KumarPandey
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think they are not visible to subclasses because it is told that they are not linked with any instances of a class but then why can we access static methods using object reference variables.
 
ravi suthar
Greenhorn
Posts: 17
Firefox Browser Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static methods cannot be override in subclass but you can define static methods in subclasses also with the same name and same method signature as defined in superclass. this is called method hiding.
 
ravi suthar
Greenhorn
Posts: 17
Firefox Browser Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static methods cannot be override in subclass but you can define static methods in subclasses also with the same name and same method signature as defined in superclass. this is called method hiding.
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This link explains this is detail:

http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are still not sure about the difference between overriding and hiding, execute the below section of code and analyze the behavior of hiding and overriding by cheking the output.



The output would be as mentioned below -

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic