6. implementation inheritance cont If a class (or a struct) also derives from interfaces, the list of base class and interfaces is separated by commas: public class MyDerivedClass: MyBaseClass , IInterface1 , IInterface2 { // etc. } For a struct, the syntax is as follows: public struct MyDerivedStruct: IInterface1 , IInterface2 { // etc. }
7. Virtual methods class MyBaseClass { public virtual string VirtualMethod() { return "This method is virtual and defined in MyBaseClass"; } } public virtual string ForeName { get { return foreName;} set { foreName = value;} } private string foreName;
8. Virtual methods cont. class MyDerivedClass: MyBaseClass { public override string VirtualMethod() { return “This method is an override defined in MyDerivedClass.”; } }
9. hiding methods class HisBaseClass { // various members } class MyDerivedClass: HisBaseClass { public int MyGroovyMethod() { // some groovy implementation return 0; } }
10. hiding methods cont. class HisBaseClass { public int MyGroovyMethod() { // some groovy implementation return 0; } // various members } class MyDerivedClass: HisBaseClass { public new int MyGroovyMethod() { // some groovy implementation return 0; } }
11. Calling base Versions of functions class CustomerAccount { public virtual decimal CalculatePrice() { // implementation return 0.0M; } } class GoldAccount: CustomerAccount { public override decimal CalculatePrice() { return base.CalculatePrice () * 0.9M; } }
12. abstract Classes and functions abstract class Building { public abstract decimal CalculateHeatingCost(); // abstract method }
13. sealed Classes and methods sealed class FinalClass { // etc } class DerivedClass: FinalClass // wrong . Will give compilation error { // etc }
14. sealed Classes and methods cont class MyClass: MyClassBase { public sealed override void FinalMethod() { // etc. } } class DerivedClass: MyClass { public override void FinalMethod() // wrong . Will give compilation error { } }
15. Constructors of derived Classes abstract class GenericCustomer { private string name; // lots of other methods etc. } class Nevermore60Customer: GenericCustomer { private uint highCostMinutesUsed; // other methods etc. } GenericCustomer customer = new Nevermore60Customer(); MortimerPhones.cs
16. Adding a Constructor in a hierarchy public abstract class GenericCustomer { private string name; public GenericCustomer() : base() // We could omit this line without affecting the compiled code . { name = "<no name>"; }
17. Adding Constructors with Parameters to a Hierarchy abstract class GenericCustomer { private string name; public GenericCustomer(string name) { this.name = name; } class Nevermore60Customer: GenericCustomer { private uint highCostMinutesUsed; public Nevermore60Customer(string name) : base(name) { }
18. Adding Constructors with Parameters to a Hierarchy cont class Nevermore60Customer: GenericCustomer { public Nevermore60Customer(string name, string referrerName) : base(name ) { this.referrerName = referrerName; } private string referrerName; private uint highCostMinutesUsed;
19. Adding Constructors with Parameters to a Hierarchy cont class Nevermore60Customer: GenericCustomer { public Nevermore60Customer(string name, string referrerName) : base(name ) { this.referrerName = referrerName; } private string referrerName; private uint highCostMinutesUsed;
20. Adding Constructors with Parameters to a Hierarchy cont public Nevermore60Customer(string name) : this(name, “<None>“) { } GenericCustomer customer = new Nevermore60Customer(“Ahmad Ezz“);
24. Interfaces public interface IDisposable { void Dispose(); } class SomeClass: IDisposable { // This class MUST contain an implementation of the // IDisposable.Dispose() method, otherwise // you get a compilation error. public void Dispose() { // implementation of Dispose() method } // rest of class }
27. Useful using IBankAccount[] accounts = new IBankAccount[2]; accounts[0] = new SaverAccount(); accounts[1] = new GoldAccount(); Note, however, that we’d get a compiler error if we tried something like this: accounts[1] = new SomeOtherClass(); // SomeOtherClass does NOT implement // IBankAccount: WRONG!! END
namespace Najah.ILoveCsharp.OOProg { using System; public abstract class GenericCustomer { private string name; public GenericCustomer() { name = &quot;<no name>&quot;; } public GenericCustomer(string name) { this.name = name; } public string Name { get {return name;} set {name = value;} } } public class Nevermore60Customer : GenericCustomer { private string referrerName; private uint highCostMinutesUsed; public Nevermore60Customer(string name) : this(name, &quot;<None>&quot;) { } public Nevermore60Customer(string name, string referrerName) : base(name) { this.referrerName = referrerName; } public string ReferrerName { get {return referrerName;} set {referrerName = value;} } } public class MainEntryPoint { public static void Main() { GenericCustomer arabel = new Nevermore60Customer(&quot;Ahmad Ezz&quot;); } } }
using System; using Najah.ILoveCsharp; using Najah.ILoveCsharp.VenusBank; using Najah.ILoveCsharp.JupiterBank; namespace Najah.ILoveCsharp { class MainEntryPoint { static void Main() { IBankAccount venusAccount = new SaverAccount(); IBankAccount jupiterAccount = new GoldAccount(); venusAccount.PayIn(200); venusAccount.Withdraw(100); Console.WriteLine(venusAccount.ToString()); jupiterAccount.PayIn(500); jupiterAccount.Withdraw(600); jupiterAccount.Withdraw(100); Console.WriteLine(jupiterAccount.ToString()); } } } namespace Najah.ILoveCsharp { public interface IBankAccount { void PayIn(decimal amount); bool Withdraw(decimal amount); decimal Balance { get; } } } namespace Najah.ILoveCsharp.VenusBank { public class SaverAccount : IBankAccount { private decimal balance; public void PayIn(decimal amount) { balance += amount; } public bool Withdraw(decimal amount) { if (balance >= amount) { balance -= amount; return true; } Console.WriteLine(&quot;Withdrawal attempt failed.&quot;); return false; } public decimal Balance { get { return balance; } } public override string ToString() { return String.Format(&quot;Venus Bank Saver: Balance = {0,6:C}&quot;, balance); } } } namespace Najah.ILoveCsharp.JupiterBank { public class GoldAccount : IBankAccount { private decimal balance; public void PayIn(decimal amount) { balance += amount; } public bool Withdraw(decimal amount) { if (balance >= amount) { balance -= amount; return true; } Console.WriteLine(&quot;Withdrawal attempt failed.&quot;); return false; } public decimal Balance { get { return balance; } } public override string ToString() { return String.Format(&quot;Jupiter Bank Saver: Balance = {0,6:C}&quot;, balance); } } }
using System; using Najah.ILoveCsharp; using Najah.ILoveCsharp.VenusBank; using Najah.ILoveCsharp.JupiterBank; namespace Najah.ILoveCsharp { class MainEntryPoint { static void Main() { IBankAccount venusAccount = new SaverAccount(); ITransferBankAccount jupiterAccount = new CurrentAccount(); venusAccount.PayIn(200); jupiterAccount.PayIn(500); jupiterAccount.TransferTo(venusAccount, 100); Console.WriteLine(venusAccount.ToString()); Console.WriteLine(jupiterAccount.ToString()); } } } namespace Najah.ILoveCsharp { public interface IBankAccount { void PayIn(decimal amount); bool Withdraw(decimal amount); decimal Balance { get; } } public interface ITransferBankAccount : IBankAccount { bool TransferTo(IBankAccount destination, decimal amount); } } namespace Najah.ILoveCsharp.VenusBank { public class SaverAccount : IBankAccount { private decimal balance; public void PayIn(decimal amount) { balance += amount; } public bool Withdraw(decimal amount) { if (balance >= amount) { balance -= amount; return true; } Console.WriteLine(&quot;Withdrawal attempt failed.&quot;); return false; } public decimal Balance { get { return balance; } } public override string ToString() { return String.Format(&quot;Venus Bank Saver: Balance = {0,6:C}&quot;, balance); } } } namespace Najah.ILoveCsharp.JupiterBank { public class CurrentAccount : ITransferBankAccount { private decimal balance; public void PayIn(decimal amount) { balance += amount; } public bool Withdraw(decimal amount) { if (balance >= amount) { balance -= amount; return true; } Console.WriteLine(&quot;Withdrawal attempt failed.&quot;); return false; } public decimal Balance { get { return balance; } } public bool TransferTo(IBankAccount destination, decimal amount) { bool result; if ((result = Withdraw(amount)) == true) destination.PayIn(amount); return result; } public override string ToString() { return String.Format(&quot;Jupiter Bank Current Account: Balance = {0,6:C}&quot;, balance); } } }