oop in csharp part1

Post on 24-Mar-2016

245 Views

Category:

Documents

4 Downloads

Preview:

Click to see full reader

DESCRIPTION

OOP in CShapr Part1

TRANSCRIPT

1

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

2

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

3

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

4

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

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.

5

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Notes:

So what is an object?

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

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Notes:

Each object defines three basic types of information that it must know:

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

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Stack: Section of process memory space where memory spaces for local variables (local to the method, such as Main method) are allocated

Heap: Section of process memory space where memory spaces for objects are allocated

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

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

9

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

10

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

11

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

12

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

13

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

14

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

15

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

16

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Consider a bank account, where account maintains a balance and also supports behavior such as, withdrawal and deposit of money.

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

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

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.

But, the variable can be used by both the methods in the classs, which are public, hence are accessible outside the class.

18

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

19

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

No memory space allocation for a property.

Memory space is allocated for the local variable ‘value’ of the ‘set’ accessor

You can think of ‘set’ accessor of ModelName property like the following method:

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

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

21

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

22

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

23

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

24

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

25

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

26

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

27

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

In this example:

1. Account is class which represents accounts of different persons

2. It has two fields, namely ‘owner’ and ‘balance’

3. Withdraw method debits some amount from your account and returns the updated current balance

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

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

29

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

30

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

31

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

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

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

33

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

34

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

35

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

36

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

37

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

38

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Note:

1. A derived class inherits most elements of its base class

2. A derived class cannot be more accessible than its base class

39

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

40

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

41

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

42

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

43

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

44

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

45

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

46

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

47

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

48

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

49

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

50

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

51

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

52

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

Method overloading: The same class has many methods with same name

Method overriding: The method name resides in the base class and the method implementations reside in the derived classes

53

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

54

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

55

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

56

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

57

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

58

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

59

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

60

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

61

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

62

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

63

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

64

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

65

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

66

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

67

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

68

PDFi

ll PD

F Ed

itor w

ith F

ree W

riter

and

Tool

s

top related