1 Reply Latest reply: Feb 17, 2004 9:02 AM by 800324 RSS

    overiding in java

    807582
      i would like to know, how to call a overidding method in the super class which is an abstract class. Since we cannot create instance for an abstract class, how to call a method in the super class without calling its implementation in subclass.
      can anyone help me?

      thanx in advance.
        • 1. Re: overiding in java
          800324
          Hi.

          There is no way to do this in general (it can be achieved to some extent for local classes, but I don't think that's what you're after).

          However, are you sure you need to do this? It implies the caller is taking on responsibilities that it shouldn't have. Sounds like a flawed design somewhere...

          One way to achieve something like what you're after is not to override at all (which means you lose the polymorphic effect). Rather than doing:
          abstract class A {
              public void doStuff() {
                 ...
              }
              ...
          }
          
          class B extends A {
             public void doStuff() {
                ...
                super.doStuff();
                ...
              }
              ...
          }
          do:
          abstract class A {
              public void doStuff() {
                 ...
              }
              ...
          }
          
          class B extends A {
             public void doMoreStuff() {
                ...
                doStuff();
                ...
              }
              ...
          }
          Rather than overriding, this leaves A.doStuff() available for clients to call as well as providing another method in B (doMoreStuff()) that has the same effect as the original overriding method.

          Regards,

          Lance