Abstract Static methods

Discussion in 'Java' started by ankur, Nov 25, 2007.

  1. ankur

    ankur Guest

    I was wondering why can't we declare abstract static methods. You see
    the Subclasses can always hide the static methods in Superclasses and
    so that way the abstract static methods of the superclass can be
    implemented in the subclass.
    ankur, Nov 25, 2007
    #1
    1. Advertising

  2. ankur

    Eric Sosman Guest

    ankur wrote:
    > I was wondering why can't we declare abstract static methods. You see
    > the Subclasses can always hide the static methods in Superclasses and
    > so that way the abstract static methods of the superclass can be
    > implemented in the subclass.


    Imagine that abstract static methods were possible, or even
    just that static methods were overridable. What output would
    you expect from

    abstract class Super {
    abstract static void method(); // just pretend ...
    }

    class Sub1 extends Super {
    static void method() {
    System.out.println("Sub1");
    }
    }

    class Sub2 extends Super {
    static void method() {
    System.out.println("Sub2");
    }
    }

    class Main {
    public static void main(String[] unused) {
    Super.method(); // what happens here?
    }
    }

    ?

    --
    Eric Sosman
    lid
    Eric Sosman, Nov 25, 2007
    #2
    1. Advertising

  3. ankur

    Lew Guest

    ankur wrote:
    >> I was wondering why can't we declare abstract static methods. You see
    >> the Subclasses [sic] can always hide the static methods in Superclasses [sic] and
    >> so that way the abstract static methods of the superclass can be
    >> implemented in the subclass.


    Hides != implements. Static methods belong to a class, not an object, and are
    not virtual, that is, they cannot be overridden.

    <http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12.4.4>

    > 15.12.4.4 Locate Method to Invoke
    > The strategy for method lookup depends on the invocation mode.
    >
    > If the invocation mode is static, no target reference is needed and overriding is not allowed.


    --
    Lew
    Lew, Nov 25, 2007
    #3
  4. ankur

    Curt Welch Guest

    Eric Sosman <> wrote:
    > ankur wrote:
    > > I was wondering why can't we declare abstract static methods. You see
    > > the Subclasses can always hide the static methods in Superclasses


    They can't really.

    If you do this:

    class A
    {
    static void f() { System.out.printf("A.f()\n"); }
    }

    class B extends A
    {
    static void f() { System.out.printf("B.f()\n"); }
    }

    {
    A a = new B();
    a.f();
    }

    Which f do you think gets called? It's A.f() not B.f(). Static methods
    don't hide, or override, each other like instance methods. Polymorphism
    doesn't work with Static methods in any real sense.

    It's an odd quirk that Java even lets you access the static methods using
    an instance object like the a variable above instead of limiting you to
    just use them as A.f(). But when you do use an instance variable like a,
    the method called has nothing to do with the run time value of the
    variable. You can do this for example and it works just fine:

    {
    A a = null;
    a.f();
    }

    You don't get a null pointer exception because the compiler is not using
    the variable "a" at run time for this call. It's just a hard-coded call to
    A.f() which the compiler lets you syntactically specify as a.f(); It just
    uses the defined type of the a variable to figure out what method to call.
    I have no clue why the designers of Java even allowed that because it is
    confusing - it makes people think it might work like instance variables
    where the type of the object at run time is going to control what method
    gets called when it doesn't.

    > and
    > > so that way the abstract static methods of the superclass can be
    > > implemented in the subclass.

    >
    > Imagine that abstract static methods were possible, or even
    > just that static methods were overridable. What output would
    > you expect from
    >
    > abstract class Super {
    > abstract static void method(); // just pretend ...
    > }


    > class Main {
    > public static void main(String[] unused) {
    > Super.method(); // what happens here?


    Compile error - Super.method is abstract.

    There's no reason the compiler couldn't allow abstract static methods.
    They would produce a compile error if the method resolved to the abstract
    version at compile time, and like normal abstract methods, it would force
    all non abstract subclasses to implement them.

    It just makes no sense to do so and would extend the confusion of thinking
    that they created some form of static polymorphism when they didn't.

    That reason you would want to use real abstract methods is so you could
    code to the superclass, and use standard polymorphism to substitute
    different subclass objects at run time. But, from the above examples, think
    about what happens when you do that with static methods.

    If you try to code to the superclass:

    method(SuperClass s)
    {
    s.staticMethodCall();
    }

    And then try to substitute a subclass object at run time:

    method(new SubClass())

    What method gets called? The SuperClass method is called no matter
    what object you pass to method() so it makes no sense to try
    and code to an Abstract SuperClass since there is no polymorphism of
    static methods. The call to the abstract static methods would
    always produce a compile error.

    --
    Curt Welch http://CurtWelch.Com/
    http://NewsReader.Com/
    Curt Welch, Nov 26, 2007
    #4
  5. ankur

    Roedy Green Guest

    On Sun, 25 Nov 2007 14:58:05 -0800 (PST), ankur
    <> wrote, quoted or indirectly quoted someone
    who said :

    >I was wondering why can't we declare abstract static methods


    Because if you ever instantiated them. they would have no relation to
    the original.

    I have suggested before that there be a way in an interface that you
    be able to specify statics that need to be implemented too. For now
    you have to handle it with documentation.

    see http://mindprod.com/jgloss/abstract.html
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Nov 26, 2007
    #5
  6. ankur

    ankur Guest

    On Nov 25, 5:49 pm, (Curt Welch) wrote:
    > Eric Sosman <> wrote:
    > > ankur wrote:
    > > > I was wondering why can't we declare abstract static methods. You see
    > > > the Subclasses can always hide the static methods in Superclasses

    >
    > They can't really.
    >
    > If you do this:
    >
    > class A
    > {
    > static void f() { System.out.printf("A.f()\n"); }
    >
    > }
    >
    > class B extends A
    > {
    > static void f() { System.out.printf("B.f()\n"); }
    >
    > }
    >
    > {
    > A a = new B();
    > a.f();
    > }
    >
    > Which f do you think gets called? It's A.f() not B.f(). Static methods
    > don't hide, or override, each other like instance methods. Polymorphism
    > doesn't work with Static methods in any real sense.
    >
    > It's an odd quirk that Java even lets you access the static methods using
    > an instance object like the a variable above instead of limiting you to
    > just use them as A.f(). But when you do use an instance variable like a,
    > the method called has nothing to do with the run time value of the
    > variable. You can do this for example and it works just fine:
    >
    > {
    > A a = null;
    > a.f();
    > }
    >
    > You don't get a null pointer exception because the compiler is not using
    > the variable "a" at run time for this call. It's just a hard-coded call to
    > A.f() which the compiler lets you syntactically specify as a.f(); It just
    > uses the defined type of the a variable to figure out what method to call.
    > I have no clue why the designers of Java even allowed that because it is
    > confusing - it makes people think it might work like instance variables
    > where the type of the object at run time is going to control what method
    > gets called when it doesn't.
    >
    > > and
    > > > so that way the abstract static methods of the superclass can be
    > > > implemented in the subclass.

    >
    > > Imagine that abstract static methods were possible, or even
    > > just that static methods were overridable. What output would
    > > you expect from

    >
    > > abstract class Super {
    > > abstract static void method(); // just pretend ...
    > > }
    > > class Main {
    > > public static void main(String[] unused) {
    > > Super.method(); // what happens here?

    >
    > Compile error - Super.method is abstract.
    >
    > There's no reason the compiler couldn't allow abstract static methods.
    > They would produce a compile error if the method resolved to the abstract
    > version at compile time, and like normal abstract methods, it would force
    > all non abstract subclasses to implement them.
    >
    > It just makes no sense to do so and would extend the confusion of thinking
    > that they created some form of static polymorphism when they didn't.
    >
    > That reason you would want to use real abstract methods is so you could
    > code to the superclass, and use standard polymorphism to substitute
    > different subclass objects at run time. But, from the above examples, think
    > about what happens when you do that with static methods.
    >
    > If you try to code to the superclass:
    >
    > method(SuperClass s)
    > {
    > s.staticMethodCall();
    > }
    >
    > And then try to substitute a subclass object at run time:
    >
    > method(new SubClass())
    >
    > What method gets called? The SuperClass method is called no matter
    > what object you pass to method() so it makes no sense to try
    > and code to an Abstract SuperClass since there is no polymorphism of
    > static methods. The call to the abstract static methods would
    > always produce a compile error.
    >
    > --
    > Curt Welch http://CurtWelch.Com/
    > http://NewsReader.Com/


    Thanks Curt for the elaborate reply. Very informative and answered my
    question really well !
    ankur, Nov 26, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DaKoadMunky
    Replies:
    4
    Views:
    537
    Lee Weiner
    Apr 20, 2004
  2. Icosahedron

    Abstract Static Methods

    Icosahedron, Jan 30, 2004, in forum: C++
    Replies:
    1
    Views:
    362
    Victor Bazarov
    Jan 30, 2004
  3. Iyer, Prasad C

    Abstract Methods & Abstract Class

    Iyer, Prasad C, Oct 20, 2005, in forum: Python
    Replies:
    0
    Views:
    525
    Iyer, Prasad C
    Oct 20, 2005
  4. Gerald Klix

    Re: Abstract Methods & Abstract Class

    Gerald Klix, Oct 20, 2005, in forum: Python
    Replies:
    1
    Views:
    335
    =?iso-8859-1?B?c+liYXN0aWVu?=
    Oct 20, 2005
  5. Tomas Mikula

    abstract static methods (again)

    Tomas Mikula, Oct 19, 2009, in forum: Java
    Replies:
    67
    Views:
    1,963
    Tomas Mikula
    Nov 4, 2009
Loading...

Share This Page