Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Why would I want to set a reference type as a superclass for an object

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know I can do that, but why? What are the advantages of doing this?
 
Marshal
Posts: 64661
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You can have several types of subclass object, all in the same reference. It is maybe easiest seen in an array

Animal[] menagerie = new Animal[]{new Dog("Spot"), new Cat("Tiddles"), new Lion("Simba")};
As you know the new Animal[] bit is redundant in that sort of declaration.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One good reason is to utilize polymorphism.
 
Bohdan Zaremba
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Now I understand that for example, a Dog can be treated as an Animal, or as a Pet. But I still wonder, why would I want to limit my methods only to Pet or Animal. First off, Dog has some of its own unique methods that neither Pet or Animal superclasses have have. Second, isn't it easier to make a Dog object, and only call on it Pet or Animal methods, instead of making an object with a reference of Pet or Animal? Can you please give me real life examples when using a superclass as a reference for an object is useful.
 
Steve Myers
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use a superclass reference to an object of a subclass, Java retains full knowledge of the class of which the object belongs. You can still access the unique methods by inserting an explicit cast like so:


This is assuming bark() is a method unique to the Dog class.

To see the benefits of why you would want the superclass reference, just google "benefits polymorphism Java" or something.
 
Bartender
Posts: 10759
68
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bohdan Zaremba wrote:Can you please give me real life examples when using a superclass as a reference for an object is useful.


It's actually more useful when the superclass is an interface, viz:

List<String> list = new ArrayList<String>();

because the interface usually specifies all public behaviour. If, later on, you decide that a linked list would be better, you can simply substitute:

List<String> list = new LinkedList<String>();

and your program will continue to work exactly as before without any other changes.

As far as class hierarchies are concerned, it also has some benefit, but usually when dealing with implementations. Continuing the above example, several of Java's List classes are actually subclasses of AbstractList, which saves the developers a lot of coding (and you, if you want to create your own); but the chances are you would use List rather than AbstractList when making references to them.

Winston
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!