Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

static and abstract methods  RSS feed

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Why cannot we have this method declaration in an abstract class ?
abstract class A {
abstract static void method();
}
ie why cannot we use both abstract and static for a method declaration ?
What I think is that when we subclass the abstract class A,
we will override method() along with the static modier as follows :
class B extends A {
static void method() {
System.out.println("Hello");
}
}
Why does this not work ?
Can anyone please let me know.
Thanks,
Gayatri
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The keyword static means that the object is created at runtime,whereas abstract classes and methods can never be instantiated they should be extended.As you see static and abstract are sort of opposites.
hence abstract and statoc wont work together.
Thats all i know,
bye
Anand Kapadi
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another point I think:
Static methods can't be overriden, while abstract methods have to be overriden(implemented). Just like finad Vs. abstract.
If I am wrong, please correct me.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Static methods can't be overriden, while abstract methods have to be overriden(implemented). Just like finad Vs. abstract.


I think you are on the right track.
Here is why I think we can not have "abstract static" method :
A static method is associated with the class and statically bound(invocations are determined at compile time). So, if you can have a reference to a concrete class but the reference type is its ancestor's abstract class, then when you invoke a static method using that reference, the static method of the abstract class will be invoked, not the concrete class. So then, if the same method is also declared abstract, the ancestor's abstract class will know nothing about the implementation on that static method. So, what would it do then? Hence, no "abstract static" for methods.
[Note that you still can have a static method in an abstract class. You just can not make it an abstract as well.]
Below is a sample code to demonstrate that point.
Hope this helps.

[ January 19, 2004: Message edited by: David Hadiprijanto ]
 
Gayatri Ganesh
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!