• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Bohdan Zaremba
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?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49793
69
  • 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.
 
Steve Myers
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10505
64
Eclipse IDE Hibernate 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