• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Csharp4 inheritance
 

Csharp4 inheritance

on

  • 806 views

 

Statistics

Views

Total Views
806
Views on SlideShare
806
Embed Views
0

Actions

Likes
1
Downloads
87
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • namespace Najah.ILoveCsharp.OOProg { using System; public abstract class GenericCustomer { private string name; public GenericCustomer() { 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, "") { } 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 Csharp4 inheritance Presentation Transcript

  • 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
    • Let’s start by reviewing exactly what C# does and does not support as far as inheritance is concerned.
    • 1- Implementation inheritance
    • 2- Interface inheritance
  • Structs and Classes
    • Structs 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 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