• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Reference and Instance variables

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I want to discuss about an example from Overriding topic. Following is the example:-



In this, b is referencing Animal but has instance of Horse. This means it will call eat() of Horse. Similarly, why can't it access b.buck(), as b has an instance of Horse class and Horse class has buck method?
Please correct me if I am wrong in giving the description and explain me how it works when we use instance of one class and refer some other.

Also , Is this also legal?

Horse b = new Animal();

Thanks in advance
 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please always write code in code tag. For code convention read this Java Programming Style
 
Jyoti Kaushal
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the link Ganish. But I did all the indenting and formatting of my code but it all vanished when it was posted. :-(
 
Marshal
Posts: 27214
87
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've added the code tags this time.

(When you post code, you'll see a "Code" button above the box you're typing in. Highlight the code and click that button to surround it with the "code" tags.)
 
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you want use method from child but use type of parent, becouse parent have not this method you get compiler error. some variant of this situation (use parent variable) you may get when pass your child to method with parent parametr.
you can't assign type of parent to variable of type child. you can do this with cast, but if you have actual object that not IS child (or sub child) you will get exception
 
Jyoti Kaushal
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, as per my understanding, method of only that class will be invoked whose object is instantiated. But only if the method is present in the referenced class. Is this right?
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Here relation between Animal and Horse is IS-A relation. Animal is super type and Horse is sub type. so All Horse can be animals but all animals can't be Horse Ex. It can be cat, dog etc.
So it is an assignment conversion where widening conversion is safe but narrowing conversions is not recommended as it's not safe. When you say Animal a, b ; These reference variables points to a type of Animal which has it's instance variable method called eat() but it doesn't have buck method which Horse has so when you try to call method buck, using Animal reference variable which has no buck method as part of Animal type, it gives above error.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
non-static methods belong to object and static members belong to class. When you want to invoke non-static methods then an object of that type(class) must be instantiated. When you try to invoke non-static method of a sub type using reference variable of super type by assigning an instance of sub type to reference variable of super type then the no-static method of sub type which you want to invoke must have to be method of super type.
 
Jyoti Kaushal
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice explanation Ganish. Thanks:-)
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to Coderanch
 
No holds barred. And no bars holed. Except this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic