File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
vatsalya rao
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • 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!
     
    Jesper de Jong
    Java Cowboy
    Saloon Keeper
    Pie
    Posts: 15150
    31
    Android IntelliJ IDE Java Scala Spring
    • 0
    • Mark post as helpful
    • send pies
    • 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
    • 0
    • Mark post as helpful
    • send pies
    • 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.
     
    dhwani mathur
    Ranch Hand
    Posts: 621
    • 0
    • Mark post as helpful
    • send pies
    • 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 !!!
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic