File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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

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();;//invokes Horse class' eat() method;

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

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

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

Why it is not the case with"carrots")?

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

Joined: Apr 20, 2006
Posts: 3381

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,


  • 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: 15081

    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 8 API documentation
    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();"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

    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:
    subject: Overloading/over riding book doubt in Bertbates book page num 310
    It's not a secret anymore!