Re: Why can't static methods be overridden?

From: Owen Jacobson (angstrom_at_lionsanctuary.net)
Date: 09/22/04

  • Next message: Owen Jacobson: "Re: Obtaining derived classes"
    Date: Wed, 22 Sep 2004 07:29:32 GMT
    
    

    On Tue, 21 Sep 2004 22:10:19 -0700, bernd_no_junk wrote:

    > Quite often I have a hierarchy of classes where
    > I want a specific accessor method to be
    > present in all classes. Therefore I specify
    > it in the base class. But the information
    > that this function returns only depends on the
    > actual class, so it should static, shouldn't it?
    >
    > I ran into the neccessity to do so sometimes
    > during design and started wondering why Java
    > actually forbids it.
    >
    > Is there a specific OO reason?

    This seems to be related to the recent discussion about invoking static
    methods through instances. Consider if derived classes could override
    static (class-scope) methods:

    class A {
      static void something () {}
    }

    class B extends A {
      static void something () {}
    }

    ...
      A anA = new B ();
      anA.something (); // This is valid, modulo B.something ()'s existence

    Which 'something' should be invoked, here? Currently the rules say that
    A.something should be invoked, but if it were possible to override
    something in derived classes you can bet people would then want to be able
    to call derived static methods through base class references, which I can
    only assume the Sun guys feel is much less straightforward than the
    current mechanism.

    For what it's worth I agree with them.

    -- 
    Some say the Wired doesn't have political borders like the real world,
    but there are far too many nonsense-spouting anarchists or idiots who
    think that pranks are a revolution. 
    

  • Next message: Owen Jacobson: "Re: Obtaining derived classes"

    Relevant Pages

    • Re: How many bytes per Italian character?
      ... override the InitState() function so it needs to be pure virtual. ... derived classes call up to them anyway; ... both the base class and derived class. ...
      (microsoft.public.vc.mfc)
    • Re: How many bytes per Italian character?
      ... override the InitState() function so it needs to be pure virtual. ... derived classes call up to them anyway; ... both the base class and derived class. ...
      (microsoft.public.vc.mfc)
    • Re: Call to Base-Class Does Not Succeed
      ... static void Main ... If you are handling this throug events as you are, then there is no need to call the base class as that has already happened by the time you get to your event handler. ... If instead of using event handlers you had overriden the OnMouseEnter method of the base class, then in your override you would want to make sure you called the base. ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: Can my class tell if one of its virtual members is overloaded?
      ... > that a base class cannot discover anything about derived ... The point is that you can never know what all the derived classes of a ... isn't another type around which *does* override a member. ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: Not pure virtual functions and mixing virtual and non virtual methods
      ... Should I call base class implementation or not? ... But doing the above assumes that one answer is good for all, that all derived classes will want the base class method called in the same way. ... More generally, the way I see it, if you combine invariant implementation with variant parts, you have no elegant way to change the variant part. ... Following them will remove all inheritance relationships. ...
      (comp.object)