aspose file tools*
The moose likes Beginning Java and the fly likes about reference type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "about reference type" Watch "about reference type" New topic
Author

about reference type

M Berg
Greenhorn

Joined: Sep 01, 2010
Posts: 13
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

Joined: Jun 27, 2008
Posts: 28
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

Joined: Sep 20, 2010
Posts: 3649
    
  17

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

Joined: Oct 13, 2005
Posts: 40052
    
  28
Welcome to JavaRanch Stephan van Hulst
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11498
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

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

Joined: Sep 20, 2010
Posts: 3649
    
  17

Thank you, Campbell and Shanky
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about reference type