Upload
abed-elazeem-bukhari
View
727
Download
0
Embed Size (px)
DESCRIPTION
Citation preview
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 inheritanceLet’s start by reviewing exactly what C# does and does not
support as far as inheritance is concerned.
1- Implementation inheritance2- Interface inheritance
Structs and ClassesStructs are always derived from System.ValueType . They
can also be derived from any number of interfaces. Classes are always derived from one other class of your
choosing. They can also be derived from any number of interfaces.
implementation inheritance
class MyDerivedClass: MyBaseClass{// functions and data members here}
implementation inheritance contIf 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 methodsclass 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 methodsclass HisBaseClass{// various members}
class MyDerivedClass: HisBaseClass{public int MyGroovyMethod(){// some groovy implementationreturn 0;}}
hiding methods cont.class HisBaseClass {public int MyGroovyMethod(){// some groovy implementationreturn 0;}// various members}
class MyDerivedClass: HisBaseClass {public new int MyGroovyMethod(){// some groovy implementationreturn 0;}}
Calling base Versions of functionsclass CustomerAccount{public virtual decimal CalculatePrice(){// implementationreturn 0.0M;}}class GoldAccount: CustomerAccount{public override decimal CalculatePrice(){return base.CalculatePrice() * 0.9M;}}
abstract Classes and functionsabstract class Building{public abstract decimal CalculateHeatingCost(); // abstract method}
sealed Classes and methodssealed class FinalClass{// etc}class DerivedClass: FinalClass // wrong. Will give compilation error{// etc}
sealed Classes and methods contclass MyClass: MyClassBase{public sealed override void FinalMethod(){// etc.}}class DerivedClass: MyClass{public override void FinalMethod() // wrong. Will give compilation error{}}
Constructors of derived Classesabstract 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 hierarchypublic abstract class GenericCustomer{private string name;public GenericCustomer(): base() // We could omit this line without affecting the compiled code.{name = "<no name>";}
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:
Interfacespublic 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