Say I have a class:
Code:
class MachoMan
{
public int x;
public virtual void changex() { x += 10; }
}
And class:
Code:
class MiniMan : MachoMan
{
public int y;
public override void changex()
{
base.changex();
y += 10;
Console.WriteLine(y);
}
}
Now I do this:
Code:
MachoMan ComboMan = new MiniMan();
Alright. If I do it doesn't work. I can understand that, it's presumably because when I made ComboMan type MachoMan, it only had the fields and methods of MiniMan. And since I made it a new MiniMan , it has the fields and methods of MachoMan, but what MiniMan has filled in those field & methods. So basically, I thought it's not suppost to have Y becuase MachoMan doesn't have that. So calling ComboMan.changex() shouldn't work because in the MiniMan class I made a new variable that MachoMan doesn't have, and manipulated it in the changex() method.
But, when I do this
Code:
ComboMan.changex();
It works fine????? Wtf? Why? If that works then that means that ComboMan still has all of the fields of MachoMan. But how can that be when I made it type MachoMan??
----------------------------------------------------------------------------------------------------------------------
I made the names different. If i'm being confusing , my question is this. So miniman inherits from machoman, and miniman has a y field which machoman does not have.
When I make comboman of type machoman but set it equal to miniman, it will have this:
Code:
public override void changex()
{
base.changex();
y += 10;
Console.WriteLine(y);
}
code inside the changex method instead of this
Code:
public virtual void changex() { x += 10; }
PLUS I am not able to do ComboMan.Y becaue it doesn't recognise it! THAT leads me to believe that ComboMan does not have all the fields and methods of MiniMan even if it is set to MiniMan. Instead it has all the fields and methods of MachoMan but they're filled in with what's in MiniMan. So instead of having the changex do the original Machoman's changex, it does what the MiniMan has filled in for it ( which is increment y with 10).
So my question is, if ComboMan doesn't have the fields and methods of MiniMan, but instead has the fields and methods of MachoMan with what MiniMan has filled in for them, then why does the changex method work? It increments y with 10, but y is a field exclusive to MiniMan. So since ComboMan doesn't have the fields and methods of MiniMan, therefore doesn't have y, it shouldn't work. So why does it work? I hope I've made myself comprehensible.