• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Regarding Arrays

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have some doubts in this program:

1)im unable to understand what would happen when we pass the doc.checkAnimals(dogs); to public void checkAnimals(Animal[] animals)
please explain in detail thanks in advance

 
MyExamCloud Software Support
Posts: 161
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here, Animal is the super class for Dog, Cat and Bird.













The checkAnimals method of AnimalDoctor takes any animal subtype and call checkup method for each animal in enhanced for loop.

The flow of method action will be like below:

pass animals array(dogs/cats/birds) to checkAnimals of AnimalDoctor > the method checkAnimals call checkup method of each animal in enhanced for loop > checkup method prints rescpective print message in console .

Thanks
 
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i would like mention there are two thing to notice

1. Up-casting of object references
a sub-class object can be treated as a super-class object .In give problem Dog ,Cat and Bird object references are up-casted to a Animal object . Therefore in follwing method one can pass object of type Animal or any subtype of Animal



2. Method resolution
it is the object at run-time determines whose checkup() is called not the reference type. In this case reference type is always of Animal type and object at run time can be of type Animal and its sub-types(Dog , Cat and Bird). Just before excuting the method the compiler dermines the object type of a and calls method of that class


there is a one trick if Dog extends Animal and Animal has no checkup() , then there will be a error.
So super type must have the method which can is called on its sub types.


 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic