Is it the right way to pass the object in reference of parent class??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • devgupta01
    New Member
    • Jan 2008
    • 20

    Is it the right way to pass the object in reference of parent class??

    One.java
    ------------

    public class One{
    int x;
    char y;
    One(){x=0;y='a' ;}
    public void disp(){System.o ut.println("x ="+x+", y ="+y);}
    }


    OneX.java
    ---------------
    public class OneX extends One{
    double z;
    OneX(){z=1.05;}
    public void dispX(){System. out.println("z ="+z);}
    public static void main(String[] aa){
    One obj1;
    obj1=new OneX();
    obj1.dispX();
    }
    }

    When I compile the OneX.java file
    then
    OneX.java: cannot resolve symbol
    symbol : method dispX ()
    location: class One
    obj1.dispX();
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Your issue here is that obj1 is defined to be an object of type One, not OneX. obj1 only knows about the methods of One, not those of OneX, because even though it's set to a OneX object, it's still treated by the compiler as a One object. I know that explanation's not all that great, if you don't get it just let me know and I'll try again...

    Comment

    • devgupta01
      New Member
      • Jan 2008
      • 20

      #3
      Originally posted by Laharl
      Your issue here is that obj1 is defined to be an object of type One, not OneX. obj1 only knows about the methods of One, not those of OneX, because even though it's set to a OneX object, it's still treated by the compiler as a One object. I know that explanation's not all that great, if you don't get it just let me know and I'll try again...
      I think that obj1 has memory of object of OneX. therefore it should has methods and fields of object of OneX.

      Thanks for answer, Please clerify me more. this is very helpful me.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        You told your compiler that your obj1 is a One object. Such an object doesn't
        have a dispX() method. The fact that you sneakily assigned an ObjX object to
        obj1 doesn't count for the compiler, i.e. your obj1 is a One object.

        kind regards,

        Jos

        Comment

        • Kid Programmer
          New Member
          • Mar 2008
          • 176

          #5
          Please put your code in code tags. It makes it much easier to read. To put your code in code tags when you are creating a new messsge click on the # sign. Type or paste your cod then click the # sign again.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by Kid Programmer
            Please put your code in code tags. It makes it much easier to read. To put your code in code tags when you are creating a new messsge click on the # sign. Type or paste your cod then click the # sign again.
            # that doesn't seem to work for me ... #

            kind regards,

            Jos

            Comment

            Working...