These are some great questions. I'll answer the first one in this post.
When inheriting from a base class, I don't fully understand the difference between using
virtual methods and the override keyword, and in using the 'new' keyword to 'hide' the base method. The book uses the term 'hide', but I find it unclear as to what this means. I understand that polymorphism only works using virtual methods and the override keyword, but I don't quite understand why this is.
Let's say you have Class A and Class B, and they both implement the same method called getName(). If B inherits from A, then if you were to call the getName() method on an object of type B, which getName would be called? Would the one in A be called, or will the one in B be called? Since your object is of type B, only B's getName() method will be called.
You can clarify by explicitly using the new keyword in the extended getName() method in B. In C#, your application will behave as expected if you forget to include the new keyword. You actually only receive a compiler warning!
For virtual, override, and polymorphism, the following example should help you out:
Try messing around with the above code and see which produces the answer you want. Only by overwriting Foo's virtual getName method, will I be able to print out
public virtual void getName()
class Zorb : Foo
public override void getName()
static void Main(string args)
// Simple foo declaration
Foo foo = new Foo();
Foo foo2 = new Zorb();
Zorb! In the second example where Foo foo2 = new Zorb(), if I don't declare getName() as having been overridden in Zorb, the output would actually be Foo! Even using the new keyword instead of override will result in Foo! being displayed.
If you have any questions, feel free to reply back.