object concepts in c# class, object, attribute, method, message, instance, encapsulation,...

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

Post on 21-Dec-2015

225 views

Category:

Documents


1 download

TRANSCRIPT

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

Object ConceptsObject Conceptsin C#in C#

Class, object, attribute, method, message, Class, object, attribute, method, message, instance, encapsulation, polymorphism, instance, encapsulation, polymorphism,

inheritance, association, persistence,inheritance, association, persistence,Generalization/specialization, subclass, Generalization/specialization, subclass,

superclasssuperclass

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

Data Encapsulation ExampleData Encapsulation Example

EdClass

main(args : string[]) : void

EncapsulatedData

x : int

X() : intX(type : int) : int

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

Data Encapsulation ExampleData Encapsulation Example

class EdClass{

[STAThread]static void Main(string[] args){

EncapsulatedData ed = new EncapsulatedData();Console.WriteLine(ed.X());

}}

Class

Method

ObjectInstantiation

Message to method X of the EncapsulatedData

Object referenced by ed

ObjectReference

Method AttributeFor Compiler

StorageClass

ReturnType

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

Data Encapsulation ExampleData 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)

ClassConstructor

Overloaded method X

Overloaded method X

Scope Modifier

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

Data Encapsulation ExampleData Encapsulation Example

Execution Yields:

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

Inheritance and PolymorphismInheritance and Polymorphism

InheritanceApplication

main(args : string[]) : void

Account

interes tRate : doublebalance : double

calcInterest() : doubles etBalance(bal : double) : voidAcount()

MoneyMarket

MoneyMarket()calcInterest() : double

Saving

Saving()calcInterest() : double

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

Inheritance and PolymorphismInheritance and Polymorphismclass 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 BindingPolymorphic

Behavior

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

Inheritance and PolymorphismInheritance and Polymorphism/// <summary>/// Account is a superclass used as a base for all types of accounts./// </summary>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

SuperclassVirtual

Method that will be

overriden

Superclass

AnnualCompounding

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

Inheritance and PolymorphismInheritance 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 InheritedFrom Account

Overridden Base Class

Method

MoneyMarket is aSpecialization of

Account

Note Semi-annualcompounding

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

Inheritance and PolymorphismInheritance 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

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

Inheritance and PolymorphismInheritance and Polymorphism

An Account object reference was used for each, but the correct interest method was

called for each. This is Polymorphism.