This discussion is archived
7 Replies Latest reply: Apr 4, 2013 5:57 PM by EJP RSS

Inheritance query

rahulras Explorer
Currently Being Moderated
Hi,

Please check the following code
public class parent {
  public void method1 (int i1) { .... }
  public void method2 (int i1, string s1) { ....  }
}

public class child1 extends parent {
  public void method2 (int i1, string s1) { ....  }
  public void method3 (string s2) { ....  }
}

public class RunAll {
  public static void main (String args[])
  {
    parent v2 = new child1();
    // case 1 : works. Parent reference calls child object's method
    v2.method2();
    // case 2 : Compilation error
    v2.method3();
  }
}
What is the theory behind case 2 failing to compile?

Thanks in advance
  • 1. Re: Inheritance query
    gimbal2 Guru
    Currently Being Moderated
    Well

    a) method3 is not visible on class parent
    b) Even if it was, you're not even passing a String parameter
  • 2. Re: Inheritance query
    836548 Journeyer
    Currently Being Moderated
    rahulras wrote:
    Hi,

    Please check the following code
    public class parent {
    public void method1 (int i1) { .... }
    public void method2 (int i1, string s1) { ....  }
    }
    
    public class child1 extends parent {
    public void method2 (int i1, string s1) { ....  }
    public void method3 (string s2) { ....  }
    }
    
    public class RunAll {
    public static void main (String args[])
    {
    parent v2 = new child1();
    // case 1 : works. Parent reference calls child object's method
    v2.method2();
    // case 2 : Compilation error
    v2.method3();
    }
    }
    What is the theory behind case 2 failing to compile?

    Thanks in advance
    V2 is the parent class reference variable , and it identify's only methods which are their defined in parent class.

    Here in above code you are trying to access a child class method by assigning the child class reference to parent class referene variable.
  • 3. Re: Inheritance query
    rp0428 Guru
    Currently Being Moderated
    >
    // case 1 : works. Parent reference calls child object's method
    v2.method2();
    >
    No - case 1 is: Parent reference calls 'parent' object's method. The parent defines 'method2' and that is the method being called.
  • 4. Re: Inheritance query
    doremifasollatido Journeyer
    Currently Being Moderated
    rp0428 wrote:
    >
    // case 1 : works. Parent reference calls child object's method
    v2.method2();
    >
    No - case 1 is: Parent reference calls 'parent' object's method. The parent defines 'method2' and that is the method being called.
    No, the OP is correct. If the parameters were specified properly (an int and a String), child1's method2 would be called, because it overrides parent's method2.
  • 5. Re: Inheritance query
    EJP Guru
    Currently Being Moderated
    What is the theory behind case 2 failing to compile?
    The theory is that there is no such method. You've called a method called method3() with no arguments. It doesn't exist.

    It has nothing to do with inheritance.
  • 6. Re: Inheritance query
    rahulras Explorer
    Currently Being Moderated
    Thanks for the replies.

    So basically, I have a reference and it is pointing to an object. Now, which methods of that object can I call? That is determined by the type of reference (pointing to the object) and NOT by the type of object. Am I correct?

    I can only call those methods which are available for the type of reference. Obviously, overriding will be considered when calling methods.
    Please correct me if I am wrong.
  • 7. Re: Inheritance query
    EJP Guru
    Currently Being Moderated
    That is determined by the type of reference (pointing to the object) and NOT by the type of object. Am I correct?
    Yes.
    Obviously, overriding will be considered when calling methods.
    Yes.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points