• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
     
    Java Cowboy
    Posts: 16084
    88
    Android Scala IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hello vatsalya,

    Please check your private messages. You can see them by clicking on "My Profile" in the top right of the page.
     
    vatsalya rao
    Ranch Hand
    Posts: 63
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Ranch Hand
    Posts: 621
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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 !!!
     
    Ew. You guys are ugly with a capital UG. Here, maybe this tiny ad can help:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic