Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

Abstract Static methods

Thread view: 
ankur - 25 Nov 2007 22:58 GMT
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.
Eric Sosman - 25 Nov 2007 23:12 GMT
> 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?
       }
    }

?

Signature

Eric Sosman
esosman@ieee-dot-org.invalid

Lew - 25 Nov 2007 23:31 GMT
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.

Signature

Lew

Curt Welch - 26 Nov 2007 01:49 GMT
> > 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
[quoted text clipped - 7 lines]
>             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.

Signature

Curt Welch                                            http://CurtWelch.Com/
curt@kcwc.com                                        http://NewsReader.Com/

ankur - 26 Nov 2007 07:19 GMT
> > > I was wondering why can't we declare abstract static methods. You see
> > > the Subclasses can always hide the static methods in Superclasses
[quoted text clipped - 94 lines]
> Curt Welch                                            http://CurtWelch.Com/
> c...@kcwc.com                                        http://NewsReader.Com/

Thanks Curt for the elaborate reply. Very informative and answered my
question really well !
Roedy Green - 26 Nov 2007 04:25 GMT
On Sun, 25 Nov 2007 14:58:05 -0800 (PST), ankur
<ankur.a.agarwal@gmail.com> 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
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.