SlideShare ist ein Scribd-Unternehmen logo
1 von 28
Abed El-Azeem Bukhari (MCPD,MCTS and MCP) el-bukhari.com
Inheritance Prepared By : Abed ElAzeem Bukhari What’s in This Chapter? -Types of inheritance - Implementing inheritance - Access modifiers - Interfaces
Types of inheritance ,[object Object],[object Object],[object Object]
Structs and Classes ,[object Object],[object Object]
implementation inheritance class  MyDerivedClass :  MyBaseClass { // functions and data members here }
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. }
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;
Virtual methods cont. class MyDerivedClass: MyBaseClass { public override string VirtualMethod() { return “This method is an override defined in MyDerivedClass.”; } }
hiding methods class HisBaseClass { // various members } class MyDerivedClass: HisBaseClass { public int MyGroovyMethod() { // some groovy implementation return 0; } }
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; } }
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; } }
abstract Classes and functions abstract  class Building { public  abstract  decimal CalculateHeatingCost(); // abstract method }
sealed Classes and methods sealed  class FinalClass { // etc } class DerivedClass: FinalClass //  wrong . Will give compilation error { // etc }
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 { } }
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
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;; }
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) { }
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;
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;
Adding Constructors with Parameters to a Hierarchy cont public Nevermore60Customer(string name) : this(name, “<None>“) { } GenericCustomer customer = new Nevermore60Customer(“Ahmad Ezz“);
Modifiers Visibility modifiers:
Modifiers cont public class OuterClass { protected  class InnerClass { // etc. } // etc. }
Modifiers cont other modifiers:
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 }
Defining and implementing interfaces cont namespace Najah.ILoveCsharp { public  interface  IBankAccount { void PayIn(decimal amount); bool Withdraw(decimal amount); decimal Balance { get; } } } BankAccounts.cs
derived interfaces namespace Najah.ILoveCsharp { public  interface  ITransferBankAccount: IBankAccount { bool TransferTo(IBankAccount destination, decimal amount); } } CurrentAccounts.cs
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
Thanks For Attending Abed El-Azeem Bukhari (MCPD,MCTS and MCP) el-bukhari.com

Weitere ähnliche Inhalte

Was ist angesagt?

Friend functions
Friend functions Friend functions
Friend functions
Megha Singh
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
Arjun Shanka
 
Inheritance
InheritanceInheritance
Inheritance
Tech_MX
 

Was ist angesagt? (20)

Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Dynamic method dispatch
Dynamic method dispatchDynamic method dispatch
Dynamic method dispatch
 
Java Programming - Inheritance
Java Programming - InheritanceJava Programming - Inheritance
Java Programming - Inheritance
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 
Friend functions
Friend functions Friend functions
Friend functions
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
 
Inheritance
InheritanceInheritance
Inheritance
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
Interface
InterfaceInterface
Interface
 
Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
 
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
 

Andere mochten auch (10)

C# basics training (Inheritance)
C# basics training (Inheritance)C# basics training (Inheritance)
C# basics training (Inheritance)
 
Inheritance in C#
Inheritance in C#Inheritance in C#
Inheritance in C#
 
Interfaces c#
Interfaces c#Interfaces c#
Interfaces c#
 
Xamarin: Inheritance and Polymorphism
Xamarin: Inheritance and PolymorphismXamarin: Inheritance and Polymorphism
Xamarin: Inheritance and Polymorphism
 
interface in c#
interface in c#interface in c#
interface in c#
 
Virtual Functions | Polymorphism | OOP
Virtual Functions | Polymorphism | OOPVirtual Functions | Polymorphism | OOP
Virtual Functions | Polymorphism | OOP
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
Inheritance
InheritanceInheritance
Inheritance
 
Chapter 7 - Data Link Control Protocols 9e
Chapter 7 - Data Link Control Protocols 9eChapter 7 - Data Link Control Protocols 9e
Chapter 7 - Data Link Control Protocols 9e
 
pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphism
 

Ähnlich wie Csharp4 inheritance

Csharp4 objects and_types
Csharp4 objects and_typesCsharp4 objects and_types
Csharp4 objects and_types
Abed Bukhari
 
Advanced programming topics asma
Advanced programming topics asmaAdvanced programming topics asma
Advanced programming topics asma
AbdullahJana
 
Chapter26 inheritance-ii
Chapter26 inheritance-iiChapter26 inheritance-ii
Chapter26 inheritance-ii
Deepak Singh
 
Java rmi example program with code
Java rmi example program with codeJava rmi example program with code
Java rmi example program with code
kamal kotecha
 
Java căn bản - Chapter4
Java căn bản - Chapter4Java căn bản - Chapter4
Java căn bản - Chapter4
Vince Vo
 

Ähnlich wie Csharp4 inheritance (20)

Csharp4 objects and_types
Csharp4 objects and_typesCsharp4 objects and_types
Csharp4 objects and_types
 
Advanced programming topics asma
Advanced programming topics asmaAdvanced programming topics asma
Advanced programming topics asma
 
Diifeerences In C#
Diifeerences In C#Diifeerences In C#
Diifeerences In C#
 
Codemotion appengine
Codemotion appengineCodemotion appengine
Codemotion appengine
 
Symfony2 - from the trenches
Symfony2 - from the trenchesSymfony2 - from the trenches
Symfony2 - from the trenches
 
Chapter26 inheritance-ii
Chapter26 inheritance-iiChapter26 inheritance-ii
Chapter26 inheritance-ii
 
OOP and C++Classes
OOP and C++ClassesOOP and C++Classes
OOP and C++Classes
 
Java
JavaJava
Java
 
Java rmi example program with code
Java rmi example program with codeJava rmi example program with code
Java rmi example program with code
 
Symfony2 from the Trenches
Symfony2 from the TrenchesSymfony2 from the Trenches
Symfony2 from the Trenches
 
Java
JavaJava
Java
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.ppt
 
Devoxx 2012 (v2)
Devoxx 2012 (v2)Devoxx 2012 (v2)
Devoxx 2012 (v2)
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
 
Csharp generics
Csharp genericsCsharp generics
Csharp generics
 
Pro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScriptPro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScript
 
From framework coupled code to #microservices through #DDD /by @codelytv
From framework coupled code to #microservices through #DDD /by @codelytvFrom framework coupled code to #microservices through #DDD /by @codelytv
From framework coupled code to #microservices through #DDD /by @codelytv
 
Java căn bản - Chapter4
Java căn bản - Chapter4Java căn bản - Chapter4
Java căn bản - Chapter4
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part I
 
Inheritance
InheritanceInheritance
Inheritance
 

Mehr von Abed Bukhari

Csharp4 arrays and_tuples
Csharp4 arrays and_tuplesCsharp4 arrays and_tuples
Csharp4 arrays and_tuples
Abed Bukhari
 
Csharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_eventsCsharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_events
Abed Bukhari
 
Csharp4 strings and_regular_expressions
Csharp4 strings and_regular_expressionsCsharp4 strings and_regular_expressions
Csharp4 strings and_regular_expressions
Abed Bukhari
 
Csharp4 operators and_casts
Csharp4 operators and_castsCsharp4 operators and_casts
Csharp4 operators and_casts
Abed Bukhari
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
Abed Bukhari
 

Mehr von Abed Bukhari (7)

Csharp4 arrays and_tuples
Csharp4 arrays and_tuplesCsharp4 arrays and_tuples
Csharp4 arrays and_tuples
 
Csharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_eventsCsharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_events
 
Csharp4 generics
Csharp4 genericsCsharp4 generics
Csharp4 generics
 
Csharp4 basics
Csharp4 basicsCsharp4 basics
Csharp4 basics
 
Csharp4 strings and_regular_expressions
Csharp4 strings and_regular_expressionsCsharp4 strings and_regular_expressions
Csharp4 strings and_regular_expressions
 
Csharp4 operators and_casts
Csharp4 operators and_castsCsharp4 operators and_casts
Csharp4 operators and_casts
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
 

Kürzlich hochgeladen

zidauu _business communication.pptx /pdf
zidauu _business  communication.pptx /pdfzidauu _business  communication.pptx /pdf
zidauu _business communication.pptx /pdf
zukhrafshabbir
 
What is paper chromatography, principal, procedure,types, diagram, advantages...
What is paper chromatography, principal, procedure,types, diagram, advantages...What is paper chromatography, principal, procedure,types, diagram, advantages...
What is paper chromatography, principal, procedure,types, diagram, advantages...
srcw2322l101
 

Kürzlich hochgeladen (20)

stock price prediction using machine learning
stock price prediction using machine learningstock price prediction using machine learning
stock price prediction using machine learning
 
Elevate Your Online Presence with SEO Services
Elevate Your Online Presence with SEO ServicesElevate Your Online Presence with SEO Services
Elevate Your Online Presence with SEO Services
 
Stages of Startup Funding - An Explainer
Stages of Startup Funding - An ExplainerStages of Startup Funding - An Explainer
Stages of Startup Funding - An Explainer
 
Making Sense of Tactile Indicators: A User-Friendly Guide
Making Sense of Tactile Indicators: A User-Friendly GuideMaking Sense of Tactile Indicators: A User-Friendly Guide
Making Sense of Tactile Indicators: A User-Friendly Guide
 
Chapter 2ppt Entrepreneurship freshman course.pptx
Chapter 2ppt Entrepreneurship freshman course.pptxChapter 2ppt Entrepreneurship freshman course.pptx
Chapter 2ppt Entrepreneurship freshman course.pptx
 
How to refresh to be fit for the future world
How to refresh to be fit for the future worldHow to refresh to be fit for the future world
How to refresh to be fit for the future world
 
The Truth About Dinesh Bafna's Situation.pdf
The Truth About Dinesh Bafna's Situation.pdfThe Truth About Dinesh Bafna's Situation.pdf
The Truth About Dinesh Bafna's Situation.pdf
 
Aspire Time & Life Leadership Workshop 2024
Aspire Time & Life Leadership Workshop 2024Aspire Time & Life Leadership Workshop 2024
Aspire Time & Life Leadership Workshop 2024
 
Copyright: What Creators and Users of Art Need to Know
Copyright: What Creators and Users of Art Need to KnowCopyright: What Creators and Users of Art Need to Know
Copyright: What Creators and Users of Art Need to Know
 
Your Work Matters to God RestorationChurch.pptx
Your Work Matters to God RestorationChurch.pptxYour Work Matters to God RestorationChurch.pptx
Your Work Matters to God RestorationChurch.pptx
 
Blinkit: Revolutionizing the On-Demand Grocery Delivery Service.pptx
Blinkit: Revolutionizing the On-Demand Grocery Delivery Service.pptxBlinkit: Revolutionizing the On-Demand Grocery Delivery Service.pptx
Blinkit: Revolutionizing the On-Demand Grocery Delivery Service.pptx
 
How to Maintain Healthy Life style.pptx
How to Maintain  Healthy Life style.pptxHow to Maintain  Healthy Life style.pptx
How to Maintain Healthy Life style.pptx
 
zidauu _business communication.pptx /pdf
zidauu _business  communication.pptx /pdfzidauu _business  communication.pptx /pdf
zidauu _business communication.pptx /pdf
 
Raising Seed Capital by Steve Schlafman at RRE Ventures
Raising Seed Capital by Steve Schlafman at RRE VenturesRaising Seed Capital by Steve Schlafman at RRE Ventures
Raising Seed Capital by Steve Schlafman at RRE Ventures
 
Aptar Closures segment - Corporate Overview-India.pdf
Aptar Closures segment - Corporate Overview-India.pdfAptar Closures segment - Corporate Overview-India.pdf
Aptar Closures segment - Corporate Overview-India.pdf
 
What is paper chromatography, principal, procedure,types, diagram, advantages...
What is paper chromatography, principal, procedure,types, diagram, advantages...What is paper chromatography, principal, procedure,types, diagram, advantages...
What is paper chromatography, principal, procedure,types, diagram, advantages...
 
FEXLE- Salesforce Field Service Lightning
FEXLE- Salesforce Field Service LightningFEXLE- Salesforce Field Service Lightning
FEXLE- Salesforce Field Service Lightning
 
Pitch Deck Teardown: Terra One's $7.5m Seed deck
Pitch Deck Teardown: Terra One's $7.5m Seed deckPitch Deck Teardown: Terra One's $7.5m Seed deck
Pitch Deck Teardown: Terra One's $7.5m Seed deck
 
Series A Fundraising Guide (Investing Individuals Improving Our World) by Accion
Series A Fundraising Guide (Investing Individuals Improving Our World) by AccionSeries A Fundraising Guide (Investing Individuals Improving Our World) by Accion
Series A Fundraising Guide (Investing Individuals Improving Our World) by Accion
 
بروفايل شركة ميار الخليج للاستشارات الهندسية.pdf
بروفايل شركة ميار الخليج للاستشارات الهندسية.pdfبروفايل شركة ميار الخليج للاستشارات الهندسية.pdf
بروفايل شركة ميار الخليج للاستشارات الهندسية.pdf
 

Csharp4 inheritance

  • 1. Abed El-Azeem Bukhari (MCPD,MCTS and MCP) el-bukhari.com
  • 2. Inheritance Prepared By : Abed ElAzeem Bukhari What’s in This Chapter? -Types of inheritance - Implementing inheritance - Access modifiers - Interfaces
  • 3.
  • 4.
  • 5. implementation inheritance class MyDerivedClass : MyBaseClass { // functions and data members here }
  • 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 &quot;This method is virtual and defined in MyBaseClass&quot;; } } 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 = &quot;<no name>&quot;; }
  • 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“);
  • 22. Modifiers cont public class OuterClass { protected class InnerClass { // etc. } // etc. }
  • 23. Modifiers cont other modifiers:
  • 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. 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. derived interfaces namespace Najah.ILoveCsharp { public interface ITransferBankAccount: IBankAccount { bool TransferTo(IBankAccount destination, decimal amount); } } CurrentAccounts.cs
  • 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. Thanks For Attending Abed El-Azeem Bukhari (MCPD,MCTS and MCP) el-bukhari.com

Hinweis der Redaktion

  1. namespace Najah.ILoveCsharp.OOProg { using System; public abstract class GenericCustomer { private string name; public GenericCustomer() { name = &amp;quot;&lt;no name&gt;&amp;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, &amp;quot;&lt;None&gt;&amp;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(&amp;quot;Ahmad Ezz&amp;quot;); } } }
  2. 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 &gt;= amount) { balance -= amount; return true; } Console.WriteLine(&amp;quot;Withdrawal attempt failed.&amp;quot;); return false; } public decimal Balance { get { return balance; } } public override string ToString() { return String.Format(&amp;quot;Venus Bank Saver: Balance = {0,6:C}&amp;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 &gt;= amount) { balance -= amount; return true; } Console.WriteLine(&amp;quot;Withdrawal attempt failed.&amp;quot;); return false; } public decimal Balance { get { return balance; } } public override string ToString() { return String.Format(&amp;quot;Jupiter Bank Saver: Balance = {0,6:C}&amp;quot;, balance); } } }
  3. 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 &gt;= amount) { balance -= amount; return true; } Console.WriteLine(&amp;quot;Withdrawal attempt failed.&amp;quot;); return false; } public decimal Balance { get { return balance; } } public override string ToString() { return String.Format(&amp;quot;Venus Bank Saver: Balance = {0,6:C}&amp;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 &gt;= amount) { balance -= amount; return true; } Console.WriteLine(&amp;quot;Withdrawal attempt failed.&amp;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(&amp;quot;Jupiter Bank Current Account: Balance = {0,6:C}&amp;quot;, balance); } } }