object concepts in c# class, object, attribute, method, message, instance, encapsulation,...
Post on 21-Dec-2015
225 views
TRANSCRIPT
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
Data Encapsulation ExampleData Encapsulation Example
EdClass
main(args : string[]) : void
EncapsulatedData
x : int
X() : intX(type : int) : int
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
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
Data Encapsulation ExampleData Encapsulation Example
Execution Yields:
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
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
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
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
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
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.