Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why it is illegal to use abstract and static modifiers together in method declaration ?

 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know about this that why it is illegal. i don't think it is a crime to use both together, please satisfied me with solid reason .
 
Stephan van Hulst
Bartender
Pie
Posts: 5408
52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would you implement such an abstract method? Surely not in a subclass, because static methods aren't inherited.
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote: because static methods aren't inherited.


Sorry but i did it as you can see below .It is really very easy to inherit static method.Is it not ?



 
Matthew Brown
Bartender
Posts: 4565
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But they can't be overridden, which is the important thing. An abstract method is "overidden" when the implementation is provided, but that's not possible with a static method.
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:But they can't be overridden, which is the important thing. An abstract method is "overidden" when the implementation is provided, but that's not possible with a static method.


hahaha again nice answer but i can override static method you want see ?
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:But they can't be overridden


Static method inheritance and overridden is given below dear for only you




 
Matthew Brown
Bartender
Posts: 4565
8
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not overriding. That's hiding. When you call with a StaticTest1 reference you get the StaticTest1 method. When you call with a StaticTest2 reference you get the StaticTest2 method. What you don't get is run-time polymorphism, which is what true overriding would imply.
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:That's not overriding. That's hiding. When you call with a StaticTest1 reference you get the StaticTest1 method. When you call with a StaticTest2 reference you get the StaticTest2 method. What you don't get is run-time polymorphism, which is what true overriding would imply.


nice i was totally wrong sorry for that
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.coderanch.com/how-to/java/OverridingVsHiding



If you run this, the output is

instanceMethod() in Bar
classMethod() in Foo
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic