permaculture playing cards*
The moose likes Beginning Java and the fly likes Overloading/over riding book doubt in Bertbates book page num 310 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Overloading/over riding book doubt in Bertbates book page num 310" Watch "Overloading/over riding book doubt in Bertbates book page num 310" New topic
Author

Overloading/over riding book doubt in Bertbates book page num 310

vatsalya rao
Ranch Hand

Joined: Feb 14, 2007
Posts: 63
Hello all,
The code is as follows

Animal ah=new horse();
ah.eat();//invokes Horse class' eat() method;

Animal ah2= new horse();
ah2.eat("carrots');//given that compile error,bcos Animal class not having a eat() method accepting String paremeters.

My doubt is why ah2.eat("carrots") is not invoking eat() method in horse class that takes string as paremeters.

Bcos in the statement ah.eat()
horse class' eat() is invoked though it is animal reference.

Why it is not the case with ah2.eat("carrots")?

Bcos here also ah2 is having Animal reference though it is Horse object.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Howdy Vats rao,

"Bcos" is not the real word. Please go through this thread for avoiding such stuff.

Regarding your query please have a look at the following two points.

For Overloaded methods,

  • During compilation the compiler sees ONLY the "Reference Type" to invoke the methods.
  • At runtime to invoke the method (when its actually the time to actually transfer the control to the called method), the "actual instance" is being used!



  • In your case1,



    Here,

  • ah1 -> reference type -> belonging to Animal class
  • the object being referred by "ah1" -> of type Horse



  • In both the cases, the variable (instance) you have used to invoke was that of type "Animal".

    In case1, the Animal class does have the method "eat()" and hence there were no errors during compilation. But at the runtime the actual method being invoked was that of type "Horse" since the actual object being referred by the instance variable "ah1" is of type "Horse".

    Based on the same logic explained above, since the Animal class does NOT have a matching method "eat(String arg)" [the one which takes a String parameter], the compiler is unable to find the match! That's why you get a compiler error.

    If you instance variable is of type "Horse" itself, there are no issues.

    Hope this helps!


    Everything has got its own deadline including one's EGO!
    [CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
    Jesper de Jong
    Java Cowboy
    Saloon Keeper

    Joined: Aug 16, 2005
    Posts: 14114
        
      16

    Hello vatsalya,

    Please check your private messages. You can see them by clicking on "My Profile" in the top right of the page.


    Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
    Scala Notes - My blog about Scala
    vatsalya rao
    Ranch Hand

    Joined: Feb 14, 2007
    Posts: 63
    Hello Raghavan Muthu,
    First of all sorry for using thw word "bcos" instead of because in this forum.

    Coming to my doubt,I added the method eat(String S) in the Animal class and continued with
    Animal ah2=new Horse();
    ah2.eat("carrots");

    it is not giving any errors.

    So what I understood is compiler looks at the reference type (whether method is overloaded or overridden is irrelevant).

    If the reference type is having the matching method then it invokes the method in instance type class.
    dhwani mathur
    Ranch Hand

    Joined: May 08, 2007
    Posts: 621
    Hey!!!Raghvan

    i too have the same doubt as explained in above post
    by vatsalya so please if you could explain,it will be kinda
    of you.....

    one more doubt i have is if we type cast
    as shown below here Parent is super class and Child is
    sub class

    Parent p=new Child();

    here p is reference of type Parent but since it is object of
    type Child it should invoke all the methods of class
    child,even if if they are not present in Parent class


    but you said that the method must be present in Parent class
    Why this if p is object of child class?

    One more thing to ask this is applicable
    for downward casting what if we perform upward casting?

    it will be kinder if you could explain with a example !!!
     
    Consider Paul's rocket mass heater.
     
    subject: Overloading/over riding book doubt in Bertbates book page num 310