Csharp4 inheritance

1,019 views
945 views

Published on

Published in: Business, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,019
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
101
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • namespace Najah.ILoveCsharp.OOProg { using System; public abstract class GenericCustomer { private string name; public GenericCustomer() { name = "<no name>"; } 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, "<None>") { } 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("Ahmad Ezz"); } } }
  • 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("Withdrawal attempt failed."); return false; } public decimal Balance { get { return balance; } } public override string ToString() { return String.Format("Venus Bank Saver: Balance = {0,6:C}", 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("Withdrawal attempt failed."); return false; } public decimal Balance { get { return balance; } } public override string ToString() { return String.Format("Jupiter Bank Saver: Balance = {0,6:C}", 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("Withdrawal attempt failed."); return false; } public decimal Balance { get { return balance; } } public override string ToString() { return String.Format("Venus Bank Saver: Balance = {0,6:C}", 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("Withdrawal attempt failed."); 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("Jupiter Bank Current Account: Balance = {0,6:C}", balance); } } }
  • Csharp4 inheritance

    1. 1. Abed El-Azeem Bukhari (MCPD,MCTS and MCP) el-bukhari.com
    2. 2. Inheritance Prepared By : Abed ElAzeem Bukhari What’s in This Chapter? -Types of inheritance - Implementing inheritance - Access modifiers - Interfaces
    3. 3. Types of inheritance <ul><li>Let’s start by reviewing exactly what C# does and does not support as far as inheritance is concerned. </li></ul><ul><li>1- Implementation inheritance </li></ul><ul><li>2- Interface inheritance </li></ul>
    4. 4. Structs and Classes <ul><li>Structs are always derived from System.ValueType . They can also be derived from any number of interfaces . </li></ul><ul><li>Classes are always derived from one other class of your choosing. They can also be derived from any number of interfaces. </li></ul>
    5. 5. implementation inheritance class MyDerivedClass : MyBaseClass { // functions and data members here }
    6. 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. 7. Virtual methods class MyBaseClass { public virtual string VirtualMethod() { return &quot;This method is virtual and defined in MyBaseClass&quot;; } } public virtual string ForeName { get { return foreName;} set { foreName = value;} } private string foreName;
    8. 8. Virtual methods cont. class MyDerivedClass: MyBaseClass { public override string VirtualMethod() { return “This method is an override defined in MyDerivedClass.”; } }
    9. 9. hiding methods class HisBaseClass { // various members } class MyDerivedClass: HisBaseClass { public int MyGroovyMethod() { // some groovy implementation return 0; } }
    10. 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. 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. 12. abstract Classes and functions abstract class Building { public abstract decimal CalculateHeatingCost(); // abstract method }
    13. 13. sealed Classes and methods sealed class FinalClass { // etc } class DerivedClass: FinalClass // wrong . Will give compilation error { // etc }
    14. 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. 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. 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 = &quot;<no name>&quot;; }
    17. 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. 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. 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. 20. Adding Constructors with Parameters to a Hierarchy cont public Nevermore60Customer(string name) : this(name, “<None>“) { } GenericCustomer customer = new Nevermore60Customer(“Ahmad Ezz“);
    21. 21. Modifiers Visibility modifiers:
    22. 22. Modifiers cont public class OuterClass { protected class InnerClass { // etc. } // etc. }
    23. 23. Modifiers cont other modifiers:
    24. 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 }
    25. 25. Defining and implementing interfaces cont namespace Najah.ILoveCsharp { public interface IBankAccount { void PayIn(decimal amount); bool Withdraw(decimal amount); decimal Balance { get; } } } BankAccounts.cs
    26. 26. derived interfaces namespace Najah.ILoveCsharp { public interface ITransferBankAccount: IBankAccount { bool TransferTo(IBankAccount destination, decimal amount); } } CurrentAccounts.cs
    27. 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
    28. 28. Thanks For Attending Abed El-Azeem Bukhari (MCPD,MCTS and MCP) el-bukhari.com

    ×