1
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
2
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
3
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
4
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
ls oo dT an er W rit ee Fr ith
Notes:
A class is essentially a description of how to construct an object that contains fields and methods.. It provides a sort of template for an object.
In C# the data items are called fields and the functions are called methods.
Class declarations define new reference types. A class can inherit from another class, and can implement interfaces.
Generic class declarations have one or more type parameters.
PD
Fi
ll P
DF
Ed
ito
rw
5
ls oo dT an er W rit ee Fr ith
Notes:
rw
So what is an object?
PD
Fi
ll P
DF
Ed
ito
An object embodies its own unique behavior, and each one models some object in the real world (and is therefore something that exists in time and space). A car or a book are examples of such objects. You can describe a car, repair it, drive it, and/or even sell it. You can buy a book, read a book, and/or even provide a review of the book. But in the real world, tangible items are not the only kind of objects that are of interest to us during software development. A work assignment, for example, is not something you can touch but you can describe it, discuss it, assign it and/or complete it. Book borrowing is also not tangible but you can describe it, monitor it and/or report it. Basically, anything that you can describe can be represented as an object, and that representation can be created, manipulated and destroyed to represent how you use the real object that it models.
6
ls oo dT an er W rit ee Fr ith
Notes:
Ed
ito
rw
Each object defines three basic types of information that it must know:
PD
Fi
ll P
DF
1. An object has to describe the features that will allow its users to distinguish it from other objects. It needs to have an identity. Even if two objects share the same features, each object has a unique identity. 2. An object must be able to describe itself. This type of information is stored in an object’s attributes and which form the object’s structure. 3. An object must be able to describe its current condition, called its state. Object state is sometimes represented by the values of each of its attributes. For example, a car can be brand new or worn out. Other times, the state is represented by the presence or absence of key relationships with other objects. For example, a book can be reserved or ordered. A reserved book has a relationship with the person who reserved it. An ordered book has a relationship with an order.
7
ls oo dT an er W rit ee Fr
rw
ith
Stack: Section of process memory space where memory spaces for local variables (local to the method, such as Main method) are allocated
ll P
DF
Ed
ito
Heap: Section of process memory space where memory spaces for objects are allocated
PD
Fi
Object of Car class will be created and will be placed in the heap. The reference of the object will be stored in a variable of that class and the memory space of that variable will be allocated in the stack.
8
9
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
10
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
11
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
12
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
13
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
14
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
15
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
16
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
ls oo dT an er W rit ee Fr
rw
ith
Consider a bank account, where account maintains a balance and also supports behavior such as, withdrawal and deposit of money.
PD
Fi
ll P
DF
Ed
ito
So, if you want to represent bank account through OOP, then create a class BankAccount, with a field ‘balance’ and two methods, ‘Withdraw and ‘Deposit’
17
ls oo dT an er W rit ee Fr
rw
ith
If you declare balance as private field then you are not allowing balance to be used directly from outside the class, thus essentially blocking the view of the variable.
PD
Fi
ll P
DF
Ed
ito
But, the variable can be used by both the methods in the classs, which are public, hence are accessible outside the class.
18
19
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
ls oo dT an er W rit ee Fr
ith
No memory space allocation for a property.
Ed
ito
rw
Memory space is allocated for the local variable ‘value’ of the ‘set’ accessor
DF
You can think of ‘set’ accessor of ModelName property like the following method:
PD
Fi
ll P
public void set_ModelName(string value) { modelName = value; } You can think of ‘get’ accessor of ModelName property like the following method: public string get_ModelName() { return modelName;} Actually, a property contains two methods internally like those mentioned above. Property gives you the illusion of a normal field, that is why it is a natural extension to a field. Also, property internally behaves like a method, that is why it is known ‘Property Function’
20
21
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
22
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
23
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
24
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
25
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
26
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
27
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
ls oo dT an er W rit ee Fr ith
In this example:
rw
1. Account is class which represents accounts of different persons
Ed
ito
2. It has two fields, namely ‘owner’ and ‘balance’
ll P
DF
3. Withdraw method debits some amount from your account and returns the updated current balance
PD
Fi
4. Deposit method credits some amount to your account and returns the updated current balance 5. To objects of Account class has been created, one for Mr. Ramesh and another for Mr. Suresh 6. Some amount is there in their account as Balance 7. Some amount has been debited from each of their account and updated balance has been returned by Withdraw() method 8. Notice: Balance is different for different object (as well as Owner data) 9. Balance (and Owner) is instance data, contains different values for different instances
28
29
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
30
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
31
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
ls oo dT an er W rit ee
PD
Fi
ll P
DF
Ed
ito
rw
ith
Fr
In this example: 1. Account is class which represents accounts of different persons 2. It has three fields, namely ‘owner’ and ‘balance’ and ‘interestRate’. 3. Interest rate is same for all account holders. So, it is not necessary to be a instance field, rather it is a static field. 4. Sinec, ‘iterestRate’ has been declared as a static field, it will not be part of any instance. It will be accessed using class name. 5. A static method has been provided to set the value for interest rate, too. In static method you can only use static data. 6. CalculateInterestAmountonBalance method calculates total interest amount generated for the current balance for the account holder. 7. To objects of Account class has been created, one for Mr. Ramesh and another for Mr. Suresh 8. Some amount is there in their account as Balance 9. Interest amount has been calculated based on the same interest rate for the available balance. 10. So, static member is shared amongst different user. It is not stored as a part of the instance. 11. Notice: Balance is different for different object (as well as Owner data) 12. Balance (and Owner) is instance data, contains different values for different instances
32
33
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
34
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
35
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
36
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
37
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
38
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
ls oo dT an er W rit ee Fr rw
ith
Note:
Ed
ito
1. A derived class inherits most elements of its base class
PD
Fi
ll P
DF
2. A derived class cannot be more accessible than its base class
39
40
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
41
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
42
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
43
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
44
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
45
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
46
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
47
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
48
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
49
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
50
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
51
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
52
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
ls oo dT an er W rit ee Fr
ith
Method overloading: The same class has many methods with same name
PD
Fi
ll P
DF
Ed
ito
rw
Method overriding: The method name resides in the base class and the method implementations reside in the derived classes
53
54
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
55
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
56
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
57
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
58
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
59
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
60
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
61
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
62
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
63
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
64
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
65
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
66
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
67
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT
68
DF
ll P
Fi
PD
ith
rw
ito
Ed ee
Fr er
W rit an
ls
oo
dT