• 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

Doubt in Inheritence

 
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below is the code in which under the main method, I am using the sentence Animal[] a={new Dog(), new Dog(),new Dog()};, but when trying to call the Dog method playDead() , it is not being called, why is that so?


 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Does this even compile? There is no method playDead() in Animal
 
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vishal Hegde,

You are using Animal object to access a method in Dog class. You can't access a sub classes member with a super classes object.

 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two aspects of method invocation:

  • Compile time binding, and
  • Runtime binding


  • For every method invocation, first compile time binding is done. The compiler resolves the method invocation based on the declared type of the reference on which the method is invoked, in your case, it's Animal. If the compiler can find the declaration of the method in the declared type, it will be happy. But in your case, there is no such method with the name: playDead() in Animal class, so it complains. So, the invocation fails during compile time only.

    If you add even an abstract method with the name playDead() in Animal class (the signature should be same, and Animal class should be abstract then), then the method in Dog will override that method. And then compiler would be happy with the method declaration in declared type of reference.

    Then at runtime, dynamic binding is done, where the actual method to be invoked is decided on the basis of the actual object type. Since here the actual object is of Dog type, the method invoked will be the one defined in Dog class.
     
    R. Jain
    Ranch Hand
    Posts: 375
    1
    Python Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Partheban Udayakumar wrote:
    You are using Animal object to access a method in Dog class.


    Technically, he's using Animal class reference. The actual object is of Dog type only.
     
    Vishal Hegde
    Ranch Hand
    Posts: 1087
    Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    R. Jain wrote:There are two aspects of method invocation:

  • Compile time binding, and
  • Runtime binding


  • For every method invocation, first compile time binding is done. The compiler resolves the method invocation based on the declared type of the reference on which the method is invoked, in your case, it's Animal. If the compiler can find the declaration of the method in the declared type, it will be happy. But in your case, there is no such method with the name: playDead() in Animal class, so it complains. So, the invocation fails during compile time only.

    If you add even an abstract method with the name playDead() in Animal class (the signature should be same, and Animal class should be abstract then), then the method in Dog will override that method. And then compiler would be happy with the method declaration in declared type of reference.

    Then at runtime, dynamic binding is done, where the actual method to be invoked is decided on the basis of the actual object type. Since here the actual object is of Dog type, the method invoked will be the one defined in Dog class.



    Thanks R Jain
     
    R. Jain
    Ranch Hand
    Posts: 375
    1
    Python Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Vishal Hegde wrote:
    Thanks R Jain


    You're welcome
     
    Partheban Udayakumar
    Ranch Hand
    Posts: 499
    Spring AngularJS Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    R. Jain wrote:
    Technically, he's using Animal class reference. The actual object is of Dog type only.



    Oh, I am sorry about that and thanks for noticing it and bringing it up.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic