Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about reference type

 
M Berg
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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
 
Lino Larios
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • 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 .
 
Stephan van Hulst
Bartender
Pie
Posts: 5589
54
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch Stephan van Hulst
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • 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
Bartender
Pie
Posts: 5589
54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Campbell and Shanky
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic