why can't a implment an abstract function as static?

Discussion in 'Java' started by Ed Thompson, Feb 3, 2004.

  1. Ed Thompson

    Ed Thompson Guest

    I have an abstract function that I would like to implment in a subclass
    as static, since it simply returns a static variable. The compiler
    complains. I am wondering why?

    Anyone?
     
    Ed Thompson, Feb 3, 2004
    #1
    1. Advertising

  2. Ed Thompson

    Tony Morris Guest

    "Ed Thompson" <> wrote in message
    news:9mWTb.36078$...
    >
    > I have an abstract function that I would like to implment in a subclass
    > as static, since it simply returns a static variable. The compiler
    > complains. I am wondering why?
    >
    > Anyone?


    Note: Java does not have functions, it has methods (and other things).

    All abstract and interface methods are implicitly non-static, abstract and
    public.
    An abstract method cannot be static, since abstract methods MUST be
    overridden by concrete implementations, and static methods cannot be
    overridden by definition.

    --
    Tony Morris
    (BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
    Software Engineer
    IBM Australia - Tivoli Security Software
    (2003 VTR1000F)
     
    Tony Morris, Feb 4, 2004
    #2
    1. Advertising

  3. Ed Thompson

    Pat Ryan Guest

    Java wont let you change context/from to static when overriding

    If a subclass re-declares a static method, it hides the base class method,
    it doesn't override it. Overriding only makes sense in terms of instance
    variables (think VMT) - static method implementations are determined by the
    compiler - hence hidden, not overridden -



    package a;

    class A {

    static void doSomething() {

    System.out.println("A.doSomething");

    }

    public static void main(String[] args) {

    B b = new B();

    b.doSomething();

    A a = (A) b;

    a.doSomething();

    }

    }

    class B extends A {

    static void doSomething() {

    System.out.println("B.doSomething");

    }

    }

    the code above outputs :

    B.doSomething

    A.doSomething





    "Ed Thompson" <> wrote in message
    news:9mWTb.36078$...
    >
    > I have an abstract function that I would like to implment in a subclass
    > as static, since it simply returns a static variable. The compiler
    > complains. I am wondering why?
    >
    > Anyone?
     
    Pat Ryan, Feb 4, 2004
    #3
  4. Ed Thompson

    Ed Thompson Guest

    > Note: Java does not have functions, it has methods (and other things).


    I know, I know, I just code in too many languages - job hazard (and joy)

    > An abstract method cannot be static, since abstract methods MUST be
    > overridden by concrete implementations, and static methods cannot be
    > overridden by definition.



    I was really referring to the subclass method being static:

    class a {
    abstract String getVar1();
    }

    class b extends a {
    final static String var1 = "somelocalvalue";

    static String getVar1() { return var1; }
    }

    why not? and why public only?
     
    Ed Thompson, Feb 4, 2004
    #4
  5. Ed Thompson

    Tony Morris Guest

    "Ed Thompson" <> wrote in message
    news:OWZTb.56073$...
    > > Note: Java does not have functions, it has methods (and other things).

    >
    >
    > I know, I know, I just code in too many languages - job hazard (and joy)
    >
    > > An abstract method cannot be static, since abstract methods MUST be
    > > overridden by concrete implementations, and static methods cannot be
    > > overridden by definition.

    >
    >
    > I was really referring to the subclass method being static:
    >
    > class a {
    > abstract String getVar1();
    > }
    >
    > class b extends a {
    > final static String var1 = "somelocalvalue";
    >
    > static String getVar1() { return var1; }
    > }
    >
    > why not? and why public only?


    Without repeating what is already well documented for fear of introducing
    errors, I suggest you read the Java 2 Language Specification. Things may be
    clearer and hopefully will answer your "why" questions - perhaps deeper
    thought processes are necessary.

    --
    Tony Morris
    (BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
    Software Engineer
    IBM Australia - Tivoli Security Software
    (2003 VTR1000F)
     
    Tony Morris, Feb 4, 2004
    #5
  6. Ed Thompson

    Flip Guest

    > compiler - hence hidden, not overridden -
    I'm sorry for maybe a dumb question, but what's the difference between
    hidden and overridden?
     
    Flip, Feb 4, 2004
    #6
  7. Flip wrote:

    >>compiler - hence hidden, not overridden -

    >
    > I'm sorry for maybe a dumb question, but what's the difference between
    > hidden and overridden?


    Both involve name resolution / member access, but overriding implies
    polymorphic behavior whereas hiding implies non-polymorphic behavior. A
    hidden member is normally still accessible by use of an appropriately
    qualified name or other trick; an overridden member may not be directly
    accessible at all.

    In Java, overriding applies exclusively (and always) to non-static
    methods that have the same name and signature as methods in a superclass
    of their class. Any other name collision between subclasses and their
    superclasses results in hiding the superclass' relevant member.
    Accesses of hidden members can often be resolved at compile-time, but
    when there is a possibility of a method being overridden the method
    dispatch must be deferred to runtime.


    John Bollinger
     
    John C. Bollinger, Feb 4, 2004
    #7
  8. Ed Thompson <> writes:

    > why not?


    Because "static" isn't part of the method signature. You cannot have
    both a static and non-static method with the same signature.

    >and why public only?


    Because methods in interfaces are defined to be public. The point of
    using interfaces sort of implies they should be.
     
    Tor Iver Wilhelmsen, Feb 4, 2004
    #8
  9. Ed Thompson

    siddhu

    Joined:
    Feb 16, 2011
    Messages:
    1
    we cannot use static with abstract because abstract method can be overriddeen in subclass of that abstract class but static methods cannot be overridden even if u override there is no problem but u dont get output.becoz if u override the static method it will become local to that class thats all. thats why we should not use static along with abstract method .
    but we can define static methods in abstract class.
    this is the main differnce between..

    hope so this will give a good explanation....
     
    siddhu, Feb 16, 2011
    #9
  10. Ed Thompson

    lsamaha

    Joined:
    May 24, 2012
    Messages:
    1
    protected abstract valid

    Several of the responses are incorrect. While Java does not allow static abstract methods, Java does allow protected abstract methods. Java does not allow protected interface methods.

    Please check the specification and/or test your answers before posting. This thread appears prominently in search results and I was shocked no one had corrected the assertions being made here.
     
    lsamaha, May 24, 2012
    #10
    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. news.microsoft.com
    Replies:
    0
    Views:
    312
    news.microsoft.com
    Apr 15, 2005
  2. DaKoadMunky
    Replies:
    4
    Views:
    552
    Lee Weiner
    Apr 20, 2004
  3. Matthias Kaeppler
    Replies:
    1
    Views:
    442
    R.F. Pels
    May 22, 2005
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,969
    Smokey Grindel
    Dec 2, 2006
  5. news.microsoft.com
    Replies:
    0
    Views:
    103
    news.microsoft.com
    Apr 15, 2005
Loading...

Share This Page