+ 2

Is this how casting works??

Animal a = new Animal(); ((Cat)a).makeSound(); An animal object is made, yet we can use this downcasting syntax each time we want to grant a one-time use of a specific child method to the animal object (a) of the parent class, correct? Animal a = new Cat(); So here (I'm not sure) we're creating an animal object that's able to access it's cat subclass methods?

27th Nov 2016, 10:49 PM
Mista Ben
Mista Ben - avatar
7 odpowiedzi
+ 2
well if you wanted to have the animal make the sound then you could have the animals sound attached to an window.onload function then have the audio recording set to transparent so that you can add the picture of the animal
27th Nov 2016, 11:02 PM
Ian
Ian - avatar
+ 2
no problem
27th Nov 2016, 11:13 PM
Ian
Ian - avatar
+ 1
Whoa lol I'm still getting down the base language before I dive into memorizing the useful prepackaged methods and functions and stuff. But thanks I'll remember that. Is my understanding correct though?
27th Nov 2016, 11:10 PM
Mista Ben
Mista Ben - avatar
+ 1
yes it is
27th Nov 2016, 11:10 PM
Ian
Ian - avatar
+ 1
Thank you I appreciate it :)
27th Nov 2016, 11:13 PM
Mista Ben
Mista Ben - avatar
0
the first code that was actually written in SoloLearn's Java course is wrong! but it has been fixed. you can also see how to downcsst in the code below the right version is this: https://code.sololearn.com/clLKVB9CoP72/?ref=app
15th Mar 2019, 4:08 AM
salar vahidi
salar vahidi - avatar
0
This example given is not very good in my opinion. Yes it does show the syntax for down caring but because both methods in the super and child class are the same its polymorphism that's happening. You can do a.makeSound without downcasting and still get the cat class as you assigned it to the object animal and it knows what to put. A better example would be having two different method names and then call each one to see how the downcast works. Overall though still a good course and I'm loving the community and the amount of help people offer.
19th Mar 2019, 4:04 AM
Strydker