Why does this inherited method work?

Posts 16–19 of 19 · Page 2 of 2
Quote Originally Posted by Jason View Post
You need to read up on how inheritance works. Although you constructed a MiniMan instance, you stored it in a variable of type MachoMan. This is perfectly valid, however because you're now using a MachoMan variable, you no longer have access to additional methods/variables that the MiniMan class made available. You do, however, have access to the methods made available in the MachoMan base class (changex). This is where inheritance can get tricky to wrap your head around, so I recommend doing some proper reading on the subject. When you call changex(), you're actually going to call the MiniMan implementation of changex(), not the original MachoMan implementation, because your variable is an instance of the MachoMan derived class, MiniMan.
YES EXACTLY! I understand all of that! But what I don't understand is this:
because you're now using a MachoMan variable, you no longer have access to additional methods/variables that the MiniMan class made available.
Exactly! So The ComboMan variable doesn't have the y field.
I understand that since it's type MachoMan but set equal to MiniMan, it'll have the methods/variables of MachoMan, but what MiniMan has implemented for them.
But MiniMan's implementation of MachoMan's changex() method changes the y field too. But ComboMan doesn't have the y field so the MiniMan's changex() method in ComboMan shouldn't work. But it's still working no problem and I don't understand that.
Quote Originally Posted by Laslod View Post
YES EXACTLY! I understand all of that! But what I don't understand is this:
because you're now using a MachoMan variable, you no longer have access to additional methods/variables that the MiniMan class made available.
Exactly! So The ComboMan variable doesn't have the y field.
I understand that since it's type MachoMan but set equal to MiniMan, it'll have the methods/variables of MachoMan, but what MiniMan has implemented for them.
But MiniMan's implementation of MachoMan's changex() method changes the y field too. But ComboMan doesn't have the y field so the MiniMan's changex() method in ComboMan shouldn't work. But it's still working no problem and I don't understand that.
Mate, this is why I said you need to read up on inheritance. A MachoMan variable has no knowledge of the "y" variable. However, when you instantiated an instance of MiniMan, the Y variable was created and allocated, and calling overriden method of the MiniMan class are free to modify that variable. Directly, you have no access to the Y variable as the compiler has no knowledge of a "Y" variable in the "MachoMan" class.
"But ComboMan doesn't have the y field so"
(actually it does)
It's because of how you declared the variables...you actually saying 2 types in 1 lines (which is perfectly fine), but you'll get 'weird' results as far as "which methods of which class can you access"

MachoMan ComboMan = new MiniMan();

Because you set it = a 'miniman' object, it has access to the 'virtual' functions of miniman.

Since 'modifying y is working' I guess that means 'ComboMan' actually does have a 'y' variable, but you can't access it because you declared the variable of type 'machoman'.

Because you used = miniman, (the 'actual object type' is machoman , so you're allowed to use it like a 'machoman', and not like a 'miniman'), any functions marked as virtual will be called for the derived class.. This is the heart of polymorphism. In reality, you create a 'miniman' object in virtual memory, but are only allowed to access it (except virtual functions!) like it's a machoman because of how you declared the variable.
Because the function was marked as virtual, the changex is called for the type you set comboMan = to.

I think maybe
Code:
..
MiniMan comboManAsMiniman  =  CType(ComboMan,miniman); // ctype vs directcast vs etc.

comboManAsMiniMan.y = 10; // should compile?
Quote Originally Posted by abuckau907 View Post
"But ComboMan doesn't have the y field so"
(actually it does)
It's because of how you declared the variables...you actually saying 2 types in 1 lines (which is perfectly fine), but you'll get 'weird' results as far as "which methods of which class can you access"
Ooooh so you're saying ComboMan does have y but I can't access it because I set it to type MachoMan therefore I can only use the methods and properties of MachoMan with the exception of if the method/property has a virtual keyword cause then it uses the MiniMan's overridden method/property.
Thanks alot! That's all I wanted to know!
So basically, doing MachoMan ComboMan = new MiniMan(); would use the same amount of memory as doing MiniMan ComboMan = new MiniMan(); right?
Because they both have the same fields/properties and methods.

Also, when you said I made ComboMan as 2 types that makes alot of sense. Yesterday I had a class called Bungeecar that inheritted from Car.
When I did:
Car bungeecar = new Bungeecar();
And did this:
if (bungeecar is Car)
(code)
The code gets excecuted. But when I did this
if(bungeecar is Bungeecar)
(code)
The code gets executed again! So that means that bungeecar is both Car and Bungeecar, just like you said!
Posts 16–19 of 19 · Page 2 of 2

Post a Reply

Similar Threads

Talk with us