28
Abed El-Azeem Bukhari (MCPD,MCTS and MCP) el-bukhari.com

Csharp4 inheritance

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Csharp4 inheritance

Abed El-Azeem Bukhari (MCPD,MCTS and MCP)el-bukhari.com

Page 2: Csharp4 inheritance

Inheritance

Prepared By : Abed ElAzeem Bukhari

What’s in This Chapter?

-Types of inheritance- Implementing inheritance- Access modifiers- Interfaces

Page 3: Csharp4 inheritance

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

Page 4: Csharp4 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.

Page 5: Csharp4 inheritance

implementation inheritance

class MyDerivedClass: MyBaseClass{// functions and data members here}

Page 6: Csharp4 inheritance

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.}

Page 7: Csharp4 inheritance

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;

Page 8: Csharp4 inheritance

Virtual methods cont.class MyDerivedClass: MyBaseClass{public override string VirtualMethod(){return “This method is an override defined in MyDerivedClass.”;}}

Page 9: Csharp4 inheritance

hiding methodsclass HisBaseClass{// various members}

class MyDerivedClass: HisBaseClass{public int MyGroovyMethod(){// some groovy implementationreturn 0;}}

Page 10: Csharp4 inheritance

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

Page 11: Csharp4 inheritance

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

Page 12: Csharp4 inheritance

abstract Classes and functionsabstract class Building{public abstract decimal CalculateHeatingCost(); // abstract method}

Page 13: Csharp4 inheritance

sealed Classes and methodssealed class FinalClass{// etc}class DerivedClass: FinalClass // wrong. Will give compilation error{// etc}

Page 14: Csharp4 inheritance

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{}}

Page 15: Csharp4 inheritance

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

Page 16: Csharp4 inheritance

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

Page 17: Csharp4 inheritance

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

Page 18: Csharp4 inheritance

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;

Page 19: Csharp4 inheritance

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;

Page 20: Csharp4 inheritance

Adding Constructors with Parameters to a Hierarchy cont

public Nevermore60Customer(string name): this(name, “<None>“){}

GenericCustomer customer = new Nevermore60Customer(“Ahmad Ezz“);

Page 21: Csharp4 inheritance

Modifiers

Visibility modifiers:

Page 22: Csharp4 inheritance

Modifiers cont

public class OuterClass{protected class InnerClass{// etc.}// etc.}

Page 23: Csharp4 inheritance

Modifiers cont

other modifiers:

Page 24: Csharp4 inheritance

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}

Page 25: Csharp4 inheritance

Defining and implementing interfaces cont

namespace Najah.ILoveCsharp{public interface IBankAccount{void PayIn(decimal amount);bool Withdraw(decimal amount);decimal Balance{get;}}}

BankAccounts.cs

Page 26: Csharp4 inheritance

derived interfaces

namespace Najah.ILoveCsharp{public interface ITransferBankAccount: IBankAccount{bool TransferTo(IBankAccount destination, decimal amount);}}

CurrentAccounts.cs

Page 27: Csharp4 inheritance

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

Page 28: Csharp4 inheritance

Thanks For Attending

Abed El-Azeem Bukhari (MCPD,MCTS and MCP)

el-bukhari.com