private static void BabyAccountExample()
        {
            IAccount b = new BabyAccount();

            b.PayInFunds(50);
            Console.WriteLine("Balance: {0}", b.GetBalance());

            Console.WriteLine("Trying to withdraw 20");
            if (b.WithdrawFunds(20))
            {
                Console.WriteLine("Balance: {0}", b.GetBalance());
            }
            else
            {
                Console.WriteLine("Not allowed");
            }

            /* You can write the code below: */
            IAccount b2 = new BabyAccount2();

            b2.PayInFunds(50);
            Console.WriteLine("Balance: {0}", b.GetBalance());

            /* This works because, although BabyAccount2 does not have a PayInFunds method, the base class does.
             * This means that the PayInFunds method from the BankAccount class is used at this point.
             * Instances of the BabyAccount2 class have abilities which they pick up from their base class. In
             * fact, at the moment, the BabyAccount2 class has no behaviors of its own; it gets everything from
             * its base class. */
        }
Exemple #2
0
        public void InheritanceFromBaseClass()
        {
            IAccount account = new BabyAccount();

            account.PayInFunds(50);
            // This	works	because,	although	BabyAccount	does	not	have	a	PayInFunds method,	the	base	class	does.
            // This	means	that	the	PayInFunds	method	from	the BankAccount	class	is	used	at	this	point.
            // Instances	of	the	BabyAccount	class	have	abilities	which	they	pick	up	from their	base	class.
            // In	fact,	at	the	moment,	the	BabyAccount	class	has	no behaviors	of	its	own;	it	gets	everything	from	its	base	class.

            // A	program	can	use	the	is	and	as	operators	when	working	with	class	hierarchies and	interfaces.
            // The	is	operator	determines	if	the	type	of	a	given	object	is	in	a particular	class	hierarchy	or	implements
            // a	specified	interface.	You	apply	the	is operator	between	a	reference	variable	and	a	type	or
            // interface	and	the	operator returns	true	if	the	reference	can	be	made	to	refer	to	objects	of	that	type.
            if (account is IAccount)
            {
                Console.WriteLine("acc is IAccount");
            }
        }