• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

casting a subclass as a superclass

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Steve Hall
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 61
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Rancher
Posts: 175
Clojure Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
You get good luck from rubbing the belly of a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic