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

casting a subclass as a superclass

Steve Hall
Greenhorn

Joined: Feb 28, 2011
Posts: 3
Ok so I found a post on here that helped me out a little.

Say I have:




now say I make an Array of type Electronics



as I understand it if I try doing gadgets[0]. i would only get the methods in the Electronics class unless I casted it as a type Television.

Why then would you want to make an array that can contain multiple types? I mean it seems useful at first. I could have an array of electronics that contains televisions, computers, phones and so forth. But if I can't call the methods of these specific types why bother? In normal implementation do you cast it as the appropriate type every time? I could see this leading to mismatching problems pretty easily unless you are very careful.

I think I have the what happens part down I just am not sure about the reason why this is useful, why would I ever type a subclass object as a superclass if i can't access it's methods.

Forgive me if I get some terminology wrong this is my first week in learning Java.
Siddhesh Deodhar
Ranch Hand

Joined: Mar 05, 2009
Posts: 117
Welcome to Ranch fourm.

Its bit abnormal to see an array containing objects of both sub type and super types. But its legal.
It feel it depends on your work/design requirement how and why you will use it.

To avoid exception in such cases, Its advisable to make use of "instanceof" operator before calling
any method on the object contained in array. So in your case if you want to call any method from Televison class
then you will check whether object you are getting from array is instanceof Televison class.


Good, Better, Best, Don't take rest until, Good becomes Better, and Better becomes Best.
Sidd : (SCJP 6 [90%] )
Steve Hall
Greenhorn

Joined: Feb 28, 2011
Posts: 3
sorry i meant having two subclasses in the array.

So superclass is electronics

with a subclass of TV, and Computer

The array contains both TV's and Computers. So the array is of the electronics type.

Since it is of type electronics you can put both TV's and Computers inside of it. However you can't access the methods for TV or Computer. Just the Electronics methods.

Maybe it's just the book, but it made it sound that it was useful to have a bunch of different related subclass objects inside one array. So instead of having a separate array for each subclass there is just one big overreaching one. So say i had subclasses TV, Computer, Phone, Stereo. And I want to put all of them in one big array. Say it was all the electronics in my home and I wanted to perform something on all of them.

I have an array of type Electronics and it contains a bunch of different objects inside of it



in this situation would I use abstract methods for Electronics? It just seems like it would be useful to be able to group everything together into one array, but not at the expense of destroying functionality. I am just worried it could get messy if there are a ton of subclass types that have specific methods volumeUp, volumeDown, goToSleep, I wouldn't want to have a giant list of abstract methods in the Electronics class I don't think.

I guess it is mainly a lack of me knowing real world project structure. I can get it to work, but I like knowing the right way to get it to work.
Anupam Jain
Ranch Hand

Joined: Mar 16, 2010
Posts: 61

Steve Hall wrote:
Why then would you want to make an array that can contain multiple types? I mean it seems useful at first. I could have an array of electronics that contains televisions, computers, phones and so forth. But if I can't call the methods of these specific types why bother? In normal implementation do you cast it as the appropriate type every time? I could see this leading to mismatching problems pretty easily unless you are very careful.

I think I have the what happens part down I just am not sure about the reason why this is useful, why would I ever type a subclass object as a superclass if i can't access it's methods.



Hello Steve,

Let's try to consider your problem in some real world scenario (separate from the programming thing).

Suppose you want to make your Electronics array function like a Universal Remote Control for your home, so that, it can turnOn() or turnOff() every electronic component in your house.

Now for this a single array is required, while the actual implementation for turnOn() and turnOff() would reside in individual classes which would be invoked appropriately according to the Object in Electronics array at run-time.

I hope this scenario should clarify you something...


SCJP-6.0 OCPJWCD-5.0
David Byron
Rancher

Joined: Jan 20, 2009
Posts: 172

Another analogy: a beauty pageant.


Now, your question is why we would ever want to instantiate different types such as Singer or Dancer but then load them into a collection of type Contestant when doing so prevents us from calling any methods not defined in Contestant. Right? Well, consider these scenarios:

It's time for the contestants to introduce themselves:
In this case, adding a Singer and a Dancer to a collection of Contestants makes sense, because you only want to operate in terms of what all added members can do by virtue of their common nature. Later, you can do likewise with Talents:

This is just like iterating over the Contestant collection, except here we use an interface type rather than a parent or base class. Finally, when it comes time to get to know each individual performer a bit more, it might be appropriate to do this:

or perhaps reflectively

The upshot of this illustration is that you want to use the most general type that will allow you to call the methods you need to call. This provides maximum flexibility. There's a fundamental OO precept that we should code to interfaces rather than implementations. One way to observe that precept is illustrated by the first example above; another is illustrated by the second. Less flexible but sometimes worthwhile is the third. The fourth is odiferous but is occasionally the right solution.


SCJD 6, OCPJP7, Baroque Potion, G+
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Siddhesh Deodhar wrote:Welcome to Ranch fourm.
Agree
Its bit abnormal to see an array containing objects of both sub type and super types. . . .
No, it is not at all abnormal.

You do realise there is hardly ever a need to cast an object to its superclass? It already has its superclass type implicitly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: casting a subclass as a superclass