static functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • skumar22
    New Member
    • Oct 2006
    • 3

    #1

    static functions

    Hi,

    why am I not allowed to override a static function of the base class?

    class B{
    static out () {}
    }
    class D extends B {
    out() {}
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by skumar22
    Hi,

    why am I not allowed to override a static function of the base class?

    class B{
    static out () {}
    }
    class D extends B {
    out() {}
    }
    First let's make the code more legal java code by providing a return type to the method.

    Code:
    class B{
    public static void out () {}
    }
    class D extends B {
    public void out() {}
    }
    This would not compile because static means that the method is not part of the class. You cannot therefore say that because B has out(), D should have out since it extends B. The reason you cannot say this is because B does not have out(out is static in B). Besides out in B is static but in D out is not static. Which brings us to what is allowed
    Code:
    class B{
    public static void out () {}
    }
    class D extends B {
    public static void out() {}
    }

    Comment

    • skumar22
      New Member
      • Oct 2006
      • 3

      #3
      I did not understand the part where you say "static means that method is not a part of the class"
      And moreover If I do
      Code:
      class B{
      public static void out () {}
      }
      class D extends B {
      public static void out() {}
      }
      class I{
      public static void main(String[] argv) {
      B obj = new D();
      obj.out; //out of B is called but if u do something like
      
      D obj1 = new D();
      obj1.out; //out of D is called
      }
      Is it not supposed that all the functions of java are kind of virtual functions?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by skumar22
        I did not understand the part where you say "static means that method is not a part of the class"
        And moreover If I do
        Code:
        class B{
        public static void out () {}
        }
        class D extends B {
        public static void out() {}
        }
        class I{
        public static void main(String[] argv) {
        B obj = new D();
        obj.out; //out of B is called but if u do something like
        
        D obj1 = new D();
        obj1.out; //out of D is called
        }
        Is it not supposed that all the functions of java are kind of virtual functions?
        A static method is independent of the design of the class eg the main method inside any class has nothing to do with the design of that class. That is why you do not need to create objects to be able to call static methods. eg for your code above, obj.out can be (more appropriately) replaced by B.out.
        That is why out of B is called when you do
        Code:
        B obj = new D();
        obj.out;
        . B obj says that obj is a B. Any static calls on obj call methods in class B.

        about that virtual function thing, I can only say that sometimes it is dangerous to carry over constructs of one language into another.

        Comment

        Working...