Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Gábor Rácz
4,201 PointsCasting of objects - trying to turn a dog into a cat ... why?
After Craig's lesson on type casting objects and upcasting and downcasting, I read up further on the topic as I was a bit confused with it all. On the community forum I came across a link that explains the concept of upcasting/downcasting and there the author mentions that you can 'turn' one type of object into another that is, to some extent, different.
The author gives an example towards the end where he creates two different classes; Cat class and a Dog class, and mentions that he can make a dog out of a cat - using the properties form the dog class.
I understand the process, I think, but I'm confused why one would do such a thing? It seems to me quite convoluted and verbose. I suppose we could argue that there are certain properties both cats and dogs share (4 legs, teeth, two eyes, fur, tail, etc), but why complicate things? Why not just keep the specific properties within their allocated class objects, even if we have to repeat some of them? I think this would make the instances of the objects clean and clear when called.
I know I mentioned a taboo amongst programmers with the 'repetition', but I feel that certain things are okay to repeat for clarity within code ... or am I being too much of a newbie?
Please, any input or personal experience would be highly appreciated.
Here is the link to the example on upcasting/downcasting I read. http://www.cs.utexas.edu/~cannata/cs345/Class%20Notes/14%20Java%20Upcasting%20Downcasting.htm
Thanks Gábor
2 Answers
Jacob Bergdahl
29,119 PointsThere may be some odd scenarios where you would want to turn a Cat into a Dog, but that's indeed unusual. What you want to do, however, is have a parent such as Animal, which Cat and Dog inherits from. Like you say, it is indeed taboo to copy and paste code or use the same code in two places, and thus having a parent class called Animal or the like is recommended, and then having Dog and Cat inherit from it. It does make for clean code, and if you ever need to change anything, you only have to change it in one spot. Again though, just to clarify, turning a Cat into a Dog is not something that you'd do in most scenarios, and I think the author was just trying to say that it's possible, rather than that it's common.
Gábor Rácz
4,201 PointsThank you Deckey and Jacob for your answers. Appreciate it!
deckey
14,630 Pointsdeckey
14,630 PointsHi Gabor, the last example of turning dog into a cat was there just to show it is possible, to some extent. Rarely ever used in production, at least from my experience, because it can lead to more confusion than anything beneficial. Plus, it goes against any common sense, turning frogs into fish or what not.
But good to know it's possible, if ever needed. You should definitely explore common up/down casting usages, cause those are used a lot, both manual and automatic.
just my 0.02$, cheers, Deckey