• 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

about reference type

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

in some book examples a parent class is seen as child objects reference type:



when the child's type itself would be more precise reference type to pass forward:



Tiger extends Animal, so both reference types work fine as methods polymorphic parameter:



My problem is, I cannot figure out any reason for referencing a Tiger object with a Pet type reference, or with Tiger type.

Generally, is there any simple rule of thumb for choosing suitable reference variable type?

Many thanks,
Kind regards MB
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Usually is for keep the abstraction in the design and implement the functionality in different ways.
That could be one reason .
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I always use the most general type possible (within reason). If you can't foresee needing the special behaviour of a specific class, then simply use a more general reference.
This is because general types are easier to use, and if you want to change the behaviour of your class, it's easier to do so without having to change other code.

Here is an example:



In this case I used Animal as the type for pet. I could have decided to go with Tiger, but now it's easier for me to change what kind of animal I want to play with, without having to change the declaration.
For instance, I can decide that Tigers are too dangerous to play with, then I just replace



with



Just do it within reason. I think the Collections API is a good example. I always use a general type like List or Set or Map, instead of ArrayList, TreeSet or LinkedHashMap. In most cases I could use the even more general type Collection, or even Iterable, but I think that a class like List conveys what I want to achieve much better.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch Stephan van Hulst
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sometimes you don't know what you're going to get. Maybe you have a collection of animals in a 'zoo'. it's much easier to say:



than do this:



And if you do use the second method, and the zookeepers add an Otter to the mix, you have to go in and write code to handle it. The first way, you never have to touch this code, regardless of what animals they add to the zoo.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:
Just do it within reason. I think the Collections API is a good example. I always use a general type like List or Set or Map, instead of ArrayList, TreeSet or LinkedHashMap. In most cases I could use the even more general type Collection, or even Iterable, but I think that a class like List conveys what I want to achieve much better.



Really good example..
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Campbell and Shanky
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic