Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Concepts in C# Class, object, attribute, method, message, instance, encapsulation, polymorphism, inheritance, association, persistence, Generalization/specialization,

Similar presentations


Presentation on theme: "Object Concepts in C# Class, object, attribute, method, message, instance, encapsulation, polymorphism, inheritance, association, persistence, Generalization/specialization,"— Presentation transcript:

1 Object Concepts in C# Class, object, attribute, method, message, instance, encapsulation, polymorphism, inheritance, association, persistence, Generalization/specialization, subclass, superclass

2 Data Encapsulation Example

3 class EdClass { [STAThread] static void Main(string[] args) { EncapsulatedData ed = new EncapsulatedData(); Console.WriteLine(ed.X()); } Class Method Object Instantiation Message to method X of the EncapsulatedData Object referenced by ed Object Reference Method Attribute For Compiler Storage Class Return Type

4 Data Encapsulation Example public class EncapsulatedData { private int x; public EncapsulatedData() { x = 25; } public int X() { return x; } public int X(int type) { return x; } Encapsulated Data (private to class) Class Constructor Overloaded method X Scope Modifier

5 Data Encapsulation Example Execution Yields:

6 Inheritance and Polymorphism

7 class InheritanceApplication { [STAThread] static void main(string[] args) { Account acc = new Account(); // create a new Account acc.setBalance(1000); Console.Write("Account interest = "); Console.WriteLine(acc.calcInterest()); acc = new MoneyMarket(); // create a new MoneyMarket object acc.setBalance(1000); Console.Write("MoneyMarket interest = "); Console.WriteLine(acc.calcInterest()); acc = new Savings(); // create a new Savings object acc.setBalance(1000); Console.Write("Savings interest = "); Console.WriteLine(acc.calcInterest()); } Create an object of type Account, MoneyMarket & Savings then calc and display interest Late Binding Polymorphic Behavior

8 Inheritance and Polymorphism /// /// Account is a superclass used as a base for all types of accounts. /// public class Account { protected double interestRate; protected double balance; public Account() // public constructor { balance = 0.0; interestRate = 0.05; } public virtual double calcInterest() // used to calculate interest { if (balance != 0.0) return balance * (1.0 + interestRate); else return 0.0; } public void setBalance(double bal) // used to set the { balance = bal; } Attributes Superclass Virtual Method that will be overriden Superclass Annual Compounding

9 Inheritance and Polymorphism public class MoneyMarket : Account // MoneyMarket is derived from the base class { public MoneyMarket() // default constructor which on the base class : base() { } public override double calcInterest() // interest semiannually compounding { if (balance != 0.0) return balance * Math.Pow((1.0 + interestRate/2.0), 2); else return 0.0; } Subclass Inherited From Account Overridden Base Class Method MoneyMarket is a Specialization of Account Note Semi-annual compounding

10 Inheritance and Polymorphism public class Savings : Account // Savings class inherits from Account { public Savings() // default constructor which call the Account { : base() } public override double calcInterest() // monthly compounding { if (balance != 0.0) return balance * Math.Pow((1.0 + interestRate/12.0), 12); else return 0.0; } Note monthly compounding

11 Inheritance and Polymorphism An Account object reference was used for each, but the correct interest method was called for each. This is Polymorphism.


Download ppt "Object Concepts in C# Class, object, attribute, method, message, instance, encapsulation, polymorphism, inheritance, association, persistence, Generalization/specialization,"

Similar presentations


Ads by Google