• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static methods can be overridden - true or false

 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the code below , when i try to override a static method, i don't get a compiler error. But i don't get the expected result.

the output is :
Mammal1 staticMethod
Mammal1 staticMethod
cow1 staticMethod

line 13 must call the sub class's static method but instead the super class method is called. so overriding doesnt work here..
so if i get a question static methods can be overridden - true or false. would the answer be False ?
 
Les Morgan
Rancher
Posts: 710
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can replace/hide them in the new object, but they are not overridden.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:line 13 must call the sub class's static method but instead the super class method is called. so overriding doesnt work here..
so if i get a question static methods can be overridden - true or false. would the answer be False ?

Only inherited instance methods can be overridden! Static (class) methods are never overridden. Never! Repeat after me: static (class) methods are never overridden

But as your code snippet illustrates, a subclass can define a static method with the same signature as a static method in the superclass. That's called hiding: the method in the subclass hides the one in the superclass. Did you know the rules for method overriding apply to these hiding static methods as well, although they can't be overridden? Read this post to find out more. So if you change the access modifier or the return type of staticMethod() in class Cow3, you'll get a compiler error.

The big difference between method overriding (instance methods) and method hiding (class/static methods) is which version of the method is executed (as you discovered as well in your code snippet):
1/ with hidden static methods, the static method of the reference type will be executed. So that's whyprints Mammal1 staticMethod. The reference type of mammalClassWithSubCons is Mammal1 and therefore its staticMethod() is executed. It doesn't matter that mammalClassWithSubCons refers to a Cow3 instance.
2/ with overridden instance methods, the instance method of the actual object (to which the reference variable is refering) will be executed.

Hope it helps!
Kind regards,
Roel
 
Puspender Tanwar
Ranch Hand
Posts: 392
2
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static methods are the class methods. That's why it is sad that these can be called by using the class name . These method are bound at the compile time only.
now coming to your, yes you can declare the static method syntax same as its super class is having, but that's not the method overriding, that is called the method hiding.

coming to your code:

1.now one thing to understand is : compiler only knows about the left hand side of the above statement, compiler doesn't know anything about the right hand side (that is a runtime process)

2. now in the above calling, when this calling statement is compiled, first the compiler check for the method, if it is present in the Mammal1 class or not (i repeat, check in Mammal1 class). Now you might be thinking that why in the Mammal1 class, just go to my point 1. remember ?.. hope you found the reason, if not .. here it is : since compiler knows nothing about the right hand side of the point 1, so only way it have to check the existance of the method is by the Mammal1 class(left side). And now since the method is static(which is bound at compile time) , is bound to the Mammal1 class, i.e calling of the Mammal1 class will be resulted . Not the Cow3 class.

Hppe it Helps
Puspender
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puspender Tanwar wrote:static methods are the class methods. That's why it is sad that these can be called by using the class name .

If static methods are class methods, why is it sad that you can call these static methods using the class name
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation...Roel, Les and Puspender.
 
Puspender Tanwar
Ranch Hand
Posts: 392
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Puspender Tanwar wrote:static methods are the class methods. That's why it is sad that these can be called by using the class name .

If static methods are class methods, why is it sad that you can call these static methods using the class name

Hello Roel, that was just to explain the OP in simple terms
Edit:
ooopppsss, sorry, that's not 'sad', that is 'said' .
That's why it is said that these can be called by using the class name
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puspender Tanwar wrote:ooopppsss, sorry, that's not 'sad', that is 'said' .
That's why it is said that these can be called by using the class name

Ah ok! No problem, we all make typos I thought you maybe wanted to say something like "static methods are the class methods. That's why it is sad that these can be called using reference variables."
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic