aspose file tools*
The moose likes Beginning Java and the fly likes Why would I want to set a reference type as a superclass for an object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why would I want to set a reference type as a superclass for an object" Watch "Why would I want to set a reference type as a superclass for an object" New topic
Author

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

Bohdan Zaremba
Greenhorn

Joined: Dec 26, 2012
Posts: 2
I know I can do that, but why? What are the advantages of doing this?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Dec 08, 2012
Posts: 47
One good reason is to utilize polymorphism.
Bohdan Zaremba
Greenhorn

Joined: Dec 26, 2012
Posts: 2
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

Joined: Dec 08, 2012
Posts: 47
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

Joined: Mar 17, 2011
Posts: 8419
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why would I want to set a reference type as a superclass for an object