• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static method in subclass

 
Trung Hieu Hoang
Greenhorn
Posts: 12
AngularJS Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,

Java is not allowed to override an instance method in subclass if it's declared as static in parent class and vice versus.

I am wondering what the reason behind that rule? Does it bring to ambiguity or something else?

Your explanation is appreciated.

Thanks,
 
Claudiu Stroe
Greenhorn
Posts: 29
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand what is your question?

You have an instance method or a static method, they are different.
If you use "static", that means it belongs to the class and you don't need an instance of that class to acces it.
If you use static methods in the parrent and in the child class, it means you are hidding, not overriding it.

 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
even this statement seems vague "if it's declared as static in parent class and vice versus"

If the question is "Why static methods cannot be overridden in the subclass"

check out these

https://www.quora.com/Why-cant-static-methods-be-overridden
http://javaconceptoftheday.com/static-binding-and-dynamic-binding-in-java/
http://stackoverflow.com/questions/2223386/why-doesnt-java-allow-overriding-of-static-methods
 
Trung Hieu Hoang
Greenhorn
Posts: 12
AngularJS Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand hiding method but for example, in this code


Static method in ParentClass supposes to stick to parent class and have nothing to do in ChildClass. But why is it now allowed?
Can you explain the reason behind?

Edit: Thanks Ramya Subraamanian. Posted before getting your reply.
 
Claudiu Stroe
Greenhorn
Posts: 29
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the method is static in parent, but not in child, it will not compile.
Vice versa
If the method is static in child, but not in parent, it will not compile.
 
Trung Hieu Hoang
Greenhorn
Posts: 12
AngularJS Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Claudiu Stroe wrote:If the method is static in parent, but not in child, it will not compile.
Vice versa
If the method is static in child, but not in parent, it will not compile.


Oops, mistype "But why is it *not allowed? ". Ramya Subraamanian links gave good answer.

Thank you,
 
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
Hi Trung Hieu Hoang,

First of all, a warm welcome to CodeRanch!

Trung Hieu Hoang wrote:Static method in ParentClass supposes to stick to parent class and have nothing to do in ChildClass. But why is it now allowed?
Can you explain the reason behind?

Tryin to explain it using my own words and maybe even a code snippet (or two).

First of all, class (static) methods are related to classes (hence their name) and instance methods are related to objects/instances (again hence their name ). Instance methods can be overridden (which is called polymorphism), class (static) methods can't. To invoke an instance method, you must have an instance. If you don't have an instance, you can't invoke an instance method. A class (static) method can be invoked without having an instance, you just use the class name. But (although not recommended) you can also invoke a class (static) method using an instance of this class. Let's illustrate with a code snippetIf you execute this code snippet, "I'm static Joey!" will be printed twice.

If you look closely at line1 in the above code snippet, this statement looks exactly the same as invoking an instance method. So assume it would be allowed to have a class (static) method and an instance method with exactly the same signature as illustrated in this code exampleBecause a class (static) and instance method have exactly the same signature, it's impossible to determine which method to execute on line1. Therefore it's not allowed to have a class (static) and instance method with exactly the same signature. And the same reason applies to a superclass and subclass. Keep in mind that if the class (static) method in the superclass is not visible to the subclass (e.g. private), the subclass can have an instance method with the same signatureThis code snippet will compile successfully (without any errors) and when executed the output will be:
I'm static Human!
I'm instance Joey!


And finally, please note that you can have a class (static) and instance method with the same name but with a different parameter list. So the method signature of both methods must be different. Here's another code snippet to illustrateThe output of this code snippet is exactly the same as the previous one

Hope it helps!
Kind regards,
Roel
 
Trung Hieu Hoang
Greenhorn
Posts: 12
AngularJS Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Roel. It's clear to me now.
 
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
Trung Hieu Hoang wrote:Thank you Roel. It's clear to me now.

You are welcome!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic