• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

static methods cant be abstract. Why?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can override static methods, then why static methods can't be declared abstract.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

you cannot have abstract static methods since there is no inheritance for static methods. Assume, we have the following situation:


If you have code like

, it is perfectly okay.
But calling

would be illegal.
If you implement a method static void foo() in your Sub-class, the static method from the superclass is not refined, it is just hidden. We do not speak of inheritance here.

HTH

Stefan
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mmmm, did you try this, Stefan? You see, static methods do inherit, and Sub.foo() is legal. The clue is in your last remark: "If you implement a method static void foo() in your Sub-class, the static method from the superclass is not refined, it is just hidden."

The reason is that static method resolution happens at compile time (early binding). With instance methods, this happens at runtime (late binding). Take, for example,Then the codeWill invoke either Super.foo() or Sub.foo(), depending on the runtime type of the object referred to by the variable "s". Not so with static methods:The same call to s.foo() will always be to Super.foo(), independent of what type of object variable "s" refers to, simply because the declared type of "s" is "Super". In fact, Super.foo() is called regardless the state of "s", even if it contains null. After all, a static method is a class method and does not belong to any specific instance. That's why, with static methods, s.foo() is bad style andIs much preferred.

With that in mind, what meaning would a static abstract method have? Binding happens early; you can't call a static abstract method in the knowledge that at run time that would actually be resolved to a concrete implementation of that method. You'd have to call the "implementation" directly so the compiler knows where it lives. But that means the entire abstract method is bypassed. In other words, with early binding, there's no way to give static abstract methods any meaning.

- Peter
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods don't inherit, they are hidden by different implementations using the same name.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods don't inherit, they are hidden by different implementations using the same name.

Errr, no, I think you mean static methods can't be overridden. They are inherited by subclasses (unless private) - that's how subclasses have unqualified access to them. Inherited != overridable. The term is officially defined in the JLS here:

Members are either declared in the type, or inherited because they are accessible members of a superclass or superinterface which are neither private nor hidden nor overridden (�8.4.6).

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
[QB]Static methods don't inherit, they are hidden by different implementations using the same name.



Ok, now I am confused. I remembered a post on the Java Radio forum by Corey McGlone who referred to 8.4.6.2 which states:

If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class. A compile-time error occurs if a static method hides an instance method.
In this respect, hiding of methods differs from hiding of fields (�8.3), for it is permissible for a static variable to hide an instance variable. Hiding is also distinct from shadowing (�6.3.1) and obscuring (�6.3.2).

A hidden method can be accessed by using a qualified name or by using a method invocation expression (�15.12) that contains the keyword super or a cast to a superclass type. In this respect, hiding of methods is similar to hiding of fields.



Is this just splitting hairs on the definition of how the child class accesses the parent static method?
 
Chris Allen
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Jim, I meant to quote you as saying

Errr, no, I think you mean static methods can't be overridden

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Back to the original Q.

static method ca not be abstract because abstract method must be implemented by the subclass. Static method must be accessable from the class.

abstract class A{
static void foo(){
System.out.println("test");
}
static abstract void foo1();
}
class B extends A{
static void foo1(){
//
}
Since static methods are called on the reference type and not on the instance of the object, u can call A.foo() but cannot call A.foo1() that violates static contract.
 
What's gotten into you? Could it be this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic