• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on abstract method  RSS feed

 
Benedict Wong
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I came across the following statements which I couldn't wrap my head around.

The abstract modifier can be used only on instance methods. It cannot be applied to static methods or to constructors.


Why is this so?

Could someone please explain to me? Thanks a lot!
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because static methods can not be overridden. If a method could be both static and abstract, how would you ever implement it?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always tell us where such quotes come from. It reduces copyright problems.
The Java® Language Specification has several sections about modifiers: this one is about methods. It lists modifiers which cannot be used together. The abstract keywords cannot be used with private or final or static because those modifiers make overriding impossible. It cannot also be used with synchronized native or strictfp because those require an implemented method to have any meaning and abstract methods don't have implementations.
 
Benedict Wong
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Please always tell us where such quotes come from. . . . .


Thanks for pointing out my misstep, Campbell! I'll take note of it in my future posts.

Also, thanks for the resource link. I shall slowly digest the information.
 
Benedict Wong
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stephan,

Thanks for taking time out to respond to my post again.

I think I have understood the concept so I will try to explain it.

An abstract method requires a subclass to override its superclass, and method overriding takes place during runtime on object instances.

A static method on the other hand, is associated to its class rather than objects - which explains why it doesn't have a this reference. Since static methods aren't associated to any objects, they cannot be overridden.

Likewise, a class has to be instantiated before the abstract modifier can work on its methods, thus the abstract modifier cannot be used on static.

Therefore I could never implement a method that is both static and abstract.

I hope I'm understanding it correctly now. Cheers!
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are. It would be better to say that binding of instance methods occurs at runtime, and binding of static methods occurs at compile time.
You can call a static method on a class which has not been instantiated. You do that all the time with utility classes.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Benedict Wong wrote:
Likewise, a class has to be instantiated before the abstract modifier can work on its methods, thus the abstract modifier cannot be used on static.

This doesn't sound right to me. You can't actually instantiate a class that has any methods declared as abstract; you can only instantiate a class if it has implemented all abstract methods that it has inherited from its superclass.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!