Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on abstract method

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Saloon Keeper
Posts: 15656
367
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79530
379
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79530
379
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
if you think brussel sprouts are yummy, you should try any other food. And this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic