1 2 Previous Next 17 Replies Latest reply: Sep 15, 2008 9:29 AM by 795426 RSS

    inheritance question

    800577
      i have two classes C and D where D extends C. there are two static methods in each with the same name m() [method hiding]. now i want to call the m() of C using an object of D. how?
        • 1. Re: inheritance question
          807589
          You cannot call a static method with an object (only as an argument) If you are using an object it only uses the type and ignores the object.
          You can call
          C.m()
          D.m()
          at any time.
          • 2. Re: inheritance question
            807589
            You can create another method in D ie.
            public void call_m() { super.m(); }
            • 3. Re: inheritance question
              800577
              you can certainly call a static method with a object!
              try this code...

              class C
              {
                 static void m()
                 { System.out.println("inside c.m()");}
              }
              
              public class Main
              {
                 public static void main(String[] args)
                 {
                    C obj = new C();
                     obj.m();
                  }
              }
              • 4. Re: inheritance question
                800577
                hello ekahraman
                there are many workarounds. i just wanted to know if there is a direct way.
                • 5. Re: inheritance question
                  807589
                  AceNeerav wrote:
                  you can certainly call a static method with a object!
                  And like I said it only takes the type and ignores the value.
                  Try this.
                  class C {
                      static void m() {
                          System.out.println("inside c.m()");
                      }
                  }
                  
                  public class Main {
                      public static void main(String[] args) {
                          C obj = null;
                          obj.m();
                      }
                  }
                  • 6. Re: inheritance question
                    807589
                    AceNeerav wrote:
                    there are many workarounds. i just wanted to know if there is a direct way.
                    do you mean like C.m() and D.m() ?
                    • 7. Re: inheritance question
                      800577
                      hi peter, that was very insightful. i never though that would work.

                      i know static methods can be invoked using class names. and that would be an easy way to do it in my case of inheritance. but just incase i want to call the method in the parent class C using a child reference, and it is a case of method hiding, how would i do it?

                      i tried this...
                      D obj = new D();
                      obj.super.m();
                      but netbeans says its an error.

                      this however executes the method inside the parent class

                       C obj = new D();
                              obj.method();
                      • 8. Re: inheritance question
                        796440
                        AceNeerav wrote:
                        this however executes the method inside the parent class

                         C obj = new D();
                        obj.method();
                        Yes, that's exactly the point. Static methods are not runtime polymorhpic. The only way to call the parent class method is to either a) use the class name explicitly, not an object reference, or b) make he method non-static.
                        • 9. Re: inheritance question
                          800577
                          ok so u mean in this particular case there is no way to use an object name right?
                          • 10. Re: inheritance question
                            807589
                            AceNeerav wrote:
                            ok so u mean in this particular case there is no way to use an object name right?
                            There's no such thing as an "object name"
                            • 11. Re: inheritance question
                              807589
                              I doubt your first question and last comment.
                              AceNeerav wrote:
                              ok so u mean in this particular case there is no way to use an object name right?
                              Even possible with using class reference ? I think no..
                              • 12. Re: inheritance question
                                800577
                                Even possible with using class reference ? I think no..


                                whats a class reference?
                                • 13. Re: inheritance question
                                  807589
                                  C obj = null;
                                  Here obj is just a reference with class type C.
                                  • 14. Re: inheritance question
                                    807589
                                    Perhaps a longer example will help.
                                    class C {
                                        static void m() {
                                            System.out.println("inside C.m()");
                                        }
                                    }
                                    class D extends C {
                                        static void m() {
                                            System.out.println("inside D.m()");
                                        }
                                    }
                                    public class Main {
                                        public static void main(String[] args) {
                                            C c = null;
                                            c.m(); // prints inside C.m();
                                            C.m(); // prints inside C.m();
                                            D d= new D();
                                            c = d;
                                            c.m(); // prints inside C.m();
                                            d.m(); // prints inside D.m();
                                            D.m(); // prints inside D.m();
                                        }
                                    }
                                    1 2 Previous Next