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?

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

C# basics training (Inheritance)
C# basics training (Inheritance)C# basics training (Inheritance)
C# basics training (Inheritance)Ankit Kashyap
 
Xamarin: Inheritance and Polymorphism
Xamarin: Inheritance and PolymorphismXamarin: Inheritance and Polymorphism
Xamarin: Inheritance and PolymorphismEng Teong Cheah
 
Virtual Functions | Polymorphism | OOP
Virtual Functions | Polymorphism | OOPVirtual Functions | Polymorphism | OOP
Virtual Functions | Polymorphism | OOPshubham ghimire
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oopsHirra Sultan
 
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 9eadpeer
 
pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismrattaj
 

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_typesAbed Bukhari
 
Advanced programming topics asma
Advanced programming topics asmaAdvanced programming topics asma
Advanced programming topics asmaAbdullahJana
 
Symfony2 - from the trenches
Symfony2 - from the trenchesSymfony2 - from the trenches
Symfony2 - from the trenchesLukas Smith
 
Chapter26 inheritance-ii
Chapter26 inheritance-iiChapter26 inheritance-ii
Chapter26 inheritance-iiDeepak Singh
 
Java rmi example program with code
Java rmi example program with codeJava rmi example program with code
Java rmi example program with codekamal kotecha
 
Symfony2 from the Trenches
Symfony2 from the TrenchesSymfony2 from the Trenches
Symfony2 from the TrenchesJonathan Wage
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.pptEmanAsem4
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPTkishu0005
 
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 TypeScriptSeok-joon Yun
 
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 @codelytvCodelyTV
 
Java căn bản - Chapter4
Java căn bản - Chapter4Java căn bản - Chapter4
Java căn bản - Chapter4Vince Vo
 
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 IEduardo Bergavera
 

Ä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_tuplesAbed Bukhari
 
Csharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_eventsCsharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_eventsAbed Bukhari
 
Csharp4 strings and_regular_expressions
Csharp4 strings and_regular_expressionsCsharp4 strings and_regular_expressions
Csharp4 strings and_regular_expressionsAbed Bukhari
 
Csharp4 operators and_casts
Csharp4 operators and_castsCsharp4 operators and_casts
Csharp4 operators and_castsAbed Bukhari
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4Abed 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

BeMetals Investor Presentation_May 3, 2024.pdf
BeMetals Investor Presentation_May 3, 2024.pdfBeMetals Investor Presentation_May 3, 2024.pdf
BeMetals Investor Presentation_May 3, 2024.pdfDerekIwanaka1
 
joint cost.pptx COST ACCOUNTING Sixteenth Edition ...
joint cost.pptx  COST ACCOUNTING  Sixteenth Edition                          ...joint cost.pptx  COST ACCOUNTING  Sixteenth Edition                          ...
joint cost.pptx COST ACCOUNTING Sixteenth Edition ...NadhimTaha
 
Structuring and Writing DRL Mckinsey (1).pdf
Structuring and Writing DRL Mckinsey (1).pdfStructuring and Writing DRL Mckinsey (1).pdf
Structuring and Writing DRL Mckinsey (1).pdflaloo_007
 
Arti Languages Pre Seed Teaser Deck 2024.pdf
Arti Languages Pre Seed Teaser Deck 2024.pdfArti Languages Pre Seed Teaser Deck 2024.pdf
Arti Languages Pre Seed Teaser Deck 2024.pdfwill854175
 
CROSS CULTURAL NEGOTIATION BY PANMISEM NS
CROSS CULTURAL NEGOTIATION BY PANMISEM NSCROSS CULTURAL NEGOTIATION BY PANMISEM NS
CROSS CULTURAL NEGOTIATION BY PANMISEM NSpanmisemningshen123
 
PHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation FinalPHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation FinalPanhandleOilandGas
 
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...Falcon Invoice Discounting
 
Mifepristone Available in Muscat +918761049707^^ €€ Buy Abortion Pills in Oman
Mifepristone Available in Muscat +918761049707^^ €€ Buy Abortion Pills in OmanMifepristone Available in Muscat +918761049707^^ €€ Buy Abortion Pills in Oman
Mifepristone Available in Muscat +918761049707^^ €€ Buy Abortion Pills in Omaninstagramfab782445
 
Cracking the 'Career Pathing' Slideshare
Cracking the 'Career Pathing' SlideshareCracking the 'Career Pathing' Slideshare
Cracking the 'Career Pathing' SlideshareWorkforce Group
 
Falcon's Invoice Discounting: Your Path to Prosperity
Falcon's Invoice Discounting: Your Path to ProsperityFalcon's Invoice Discounting: Your Path to Prosperity
Falcon's Invoice Discounting: Your Path to Prosperityhemanthkumar470700
 
TVB_The Vietnam Believer Newsletter_May 6th, 2024_ENVol. 006.pdf
TVB_The Vietnam Believer Newsletter_May 6th, 2024_ENVol. 006.pdfTVB_The Vietnam Believer Newsletter_May 6th, 2024_ENVol. 006.pdf
TVB_The Vietnam Believer Newsletter_May 6th, 2024_ENVol. 006.pdfbelieveminhh
 
Lucknow Housewife Escorts by Sexy Bhabhi Service 8250092165
Lucknow Housewife Escorts  by Sexy Bhabhi Service 8250092165Lucknow Housewife Escorts  by Sexy Bhabhi Service 8250092165
Lucknow Housewife Escorts by Sexy Bhabhi Service 8250092165meghakumariji156
 
Phases of Negotiation .pptx
 Phases of Negotiation .pptx Phases of Negotiation .pptx
Phases of Negotiation .pptxnandhinijagan9867
 
Horngren’s Cost Accounting A Managerial Emphasis, Canadian 9th edition soluti...
Horngren’s Cost Accounting A Managerial Emphasis, Canadian 9th edition soluti...Horngren’s Cost Accounting A Managerial Emphasis, Canadian 9th edition soluti...
Horngren’s Cost Accounting A Managerial Emphasis, Canadian 9th edition soluti...ssuserf63bd7
 
Putting the SPARK into Virtual Training.pptx
Putting the SPARK into Virtual Training.pptxPutting the SPARK into Virtual Training.pptx
Putting the SPARK into Virtual Training.pptxCynthia Clay
 
Escorts in Nungambakkam Phone 8250092165 Enjoy 24/7 Escort Service Enjoy Your...
Escorts in Nungambakkam Phone 8250092165 Enjoy 24/7 Escort Service Enjoy Your...Escorts in Nungambakkam Phone 8250092165 Enjoy 24/7 Escort Service Enjoy Your...
Escorts in Nungambakkam Phone 8250092165 Enjoy 24/7 Escort Service Enjoy Your...meghakumariji156
 

Kürzlich hochgeladen (20)

BeMetals Investor Presentation_May 3, 2024.pdf
BeMetals Investor Presentation_May 3, 2024.pdfBeMetals Investor Presentation_May 3, 2024.pdf
BeMetals Investor Presentation_May 3, 2024.pdf
 
joint cost.pptx COST ACCOUNTING Sixteenth Edition ...
joint cost.pptx  COST ACCOUNTING  Sixteenth Edition                          ...joint cost.pptx  COST ACCOUNTING  Sixteenth Edition                          ...
joint cost.pptx COST ACCOUNTING Sixteenth Edition ...
 
Structuring and Writing DRL Mckinsey (1).pdf
Structuring and Writing DRL Mckinsey (1).pdfStructuring and Writing DRL Mckinsey (1).pdf
Structuring and Writing DRL Mckinsey (1).pdf
 
!~+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUD...
!~+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUD...!~+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUD...
!~+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUD...
 
HomeRoots Pitch Deck | Investor Insights | April 2024
HomeRoots Pitch Deck | Investor Insights | April 2024HomeRoots Pitch Deck | Investor Insights | April 2024
HomeRoots Pitch Deck | Investor Insights | April 2024
 
Buy gmail accounts.pdf buy Old Gmail Accounts
Buy gmail accounts.pdf buy Old Gmail AccountsBuy gmail accounts.pdf buy Old Gmail Accounts
Buy gmail accounts.pdf buy Old Gmail Accounts
 
Arti Languages Pre Seed Teaser Deck 2024.pdf
Arti Languages Pre Seed Teaser Deck 2024.pdfArti Languages Pre Seed Teaser Deck 2024.pdf
Arti Languages Pre Seed Teaser Deck 2024.pdf
 
CROSS CULTURAL NEGOTIATION BY PANMISEM NS
CROSS CULTURAL NEGOTIATION BY PANMISEM NSCROSS CULTURAL NEGOTIATION BY PANMISEM NS
CROSS CULTURAL NEGOTIATION BY PANMISEM NS
 
PHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation FinalPHX May 2024 Corporate Presentation Final
PHX May 2024 Corporate Presentation Final
 
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...
Unveiling Falcon Invoice Discounting: Leading the Way as India's Premier Bill...
 
Mifepristone Available in Muscat +918761049707^^ €€ Buy Abortion Pills in Oman
Mifepristone Available in Muscat +918761049707^^ €€ Buy Abortion Pills in OmanMifepristone Available in Muscat +918761049707^^ €€ Buy Abortion Pills in Oman
Mifepristone Available in Muscat +918761049707^^ €€ Buy Abortion Pills in Oman
 
Cracking the 'Career Pathing' Slideshare
Cracking the 'Career Pathing' SlideshareCracking the 'Career Pathing' Slideshare
Cracking the 'Career Pathing' Slideshare
 
Falcon's Invoice Discounting: Your Path to Prosperity
Falcon's Invoice Discounting: Your Path to ProsperityFalcon's Invoice Discounting: Your Path to Prosperity
Falcon's Invoice Discounting: Your Path to Prosperity
 
TVB_The Vietnam Believer Newsletter_May 6th, 2024_ENVol. 006.pdf
TVB_The Vietnam Believer Newsletter_May 6th, 2024_ENVol. 006.pdfTVB_The Vietnam Believer Newsletter_May 6th, 2024_ENVol. 006.pdf
TVB_The Vietnam Believer Newsletter_May 6th, 2024_ENVol. 006.pdf
 
Lucknow Housewife Escorts by Sexy Bhabhi Service 8250092165
Lucknow Housewife Escorts  by Sexy Bhabhi Service 8250092165Lucknow Housewife Escorts  by Sexy Bhabhi Service 8250092165
Lucknow Housewife Escorts by Sexy Bhabhi Service 8250092165
 
Phases of Negotiation .pptx
 Phases of Negotiation .pptx Phases of Negotiation .pptx
Phases of Negotiation .pptx
 
Horngren’s Cost Accounting A Managerial Emphasis, Canadian 9th edition soluti...
Horngren’s Cost Accounting A Managerial Emphasis, Canadian 9th edition soluti...Horngren’s Cost Accounting A Managerial Emphasis, Canadian 9th edition soluti...
Horngren’s Cost Accounting A Managerial Emphasis, Canadian 9th edition soluti...
 
Putting the SPARK into Virtual Training.pptx
Putting the SPARK into Virtual Training.pptxPutting the SPARK into Virtual Training.pptx
Putting the SPARK into Virtual Training.pptx
 
Escorts in Nungambakkam Phone 8250092165 Enjoy 24/7 Escort Service Enjoy Your...
Escorts in Nungambakkam Phone 8250092165 Enjoy 24/7 Escort Service Enjoy Your...Escorts in Nungambakkam Phone 8250092165 Enjoy 24/7 Escort Service Enjoy Your...
Escorts in Nungambakkam Phone 8250092165 Enjoy 24/7 Escort Service Enjoy Your...
 
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabiunwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
 

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); } } }