35
1 Chapter Four Creating and Using Classes

1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

Embed Size (px)

Citation preview

Page 1: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

1

Chapter Four

Creating and Using Classes

Page 2: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

2

Objectives

• Learn about class concepts• How to create a class from which objects can

be instantiated• Learn about instance variables and methods• How to declare objects

Page 3: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

3

Objectives

• How to compile and run a program that instantiates class objects

• How to organize your classes• Learn about public fields and private methods• Learn about the this reference

Page 4: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

4

Objectives

• How to write constructor methods• How to pass parameters to constructors• How to overload constructors• How to write destructor methods

Page 5: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

5

Understanding Class Concepts

• There are two distinct types of classes created in C#:– Classes that contain a Main() method– Classes from which you instantiate objects

• Everything is considered an object in the object-oriented approach

• Object-oriented programmers recognize “is-a relationships”

Page 6: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

6

Understanding Class Concepts

• The use of objects provides a better understanding of the idea that is being programmed

• The data components of a class are called the instance variables of that class

• Class object attributes are called fields• The contents of a class object’s instance variables are

also known as its state• In addition to attributes, class objects also have methods

associated with them

Page 7: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

7

Creating a Class from Which Objects Can Be Instantiated

• A class header or class definition contains three parts:– An optional access modifier– The keyword class– Any legal identifier you choose for the name of your

class

Page 8: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

8

Creating a Class from Which Objects Can Be Instantiated

• Private and protected classes have limited uses• When you declare a class using a namespace,

you can only declare it to be public or internal• Class access is internal by default

Page 9: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

9

Creating Instance Variables and Methods

• Instance variables are created using the same syntax used to declare other variables

• The allowable field modifiers are new, public, protected, internal, private, static, readonly, and volatile

Page 10: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

10

Creating Instance Variables and Methods

• Identifying a field as private means that no other class can access the field’s value, and only methods of the same class will be allowed to set, get, or otherwise use the field

• Using private fields within classes is an example of information hiding

• Although most fields are private, most class methods are public

Page 11: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

11

Creating Instance Variables and Methods

• In the following code, the variable idNumber is private, yet remains accessible to outside classes through the GetId() method

• Methods used with object instantiations are called instance methods

Page 12: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

12

Creating Instance Variables and Methods

• Figure 4-4 creates a method that is a counterpart to the GetID() method, called SetId()

• The SetId() method does not return any value, rather, it is used to assign a value in the class

Page 13: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

13

Declaring Objects

• Declaring a class does not create any actual objects• A two step process creates an object that is an instance

of a class:– Supply a type and an identifier– Allocate computer memory for the object

• Declaring an object notifies the compiler of the identifier and the type, but it does not allocate memory

Page 14: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

14

Declaring Objects

• To allocate the needed memory for an object, use the new operator

• For example:

Employee myAssistant = new Employee();• The value being assigned to myAssistant is a memory

address at which it will be located• Any class created can be referred to as a reference

type, while the predefined types are called value types• The method called after the new keyword is called the

constructor method

Page 15: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

15

Compiling and Running a Program That Instantiates Class Objects

• When you create an application that uses multiple class definitions, the class definitions can be stored in either a single file or each can be placed in its own file

• Storing all class definitions in one file shortens development time and simplifies the compilation process

• Using separate files for each class definition provides a more organized and manageable technique, and it allows for easier reusability

Page 16: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

16

Organizing Your Classes

• Most classes you create will have multiple methods and fields, which can become difficult to track in large programs

• It is a good idea to arrange fields and methods in a logical order:– Alphabetically– By “Sets” and “Gets”– Pairing of “Sets” and “Gets”

Page 17: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

17

Organizing Your Classes

• In addition to organizing fields and methods, comments should also be used

Page 18: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

18

Using Public Fields and Private Methods

• The private fields/public method arrangement ensures that data will be used and changed only in ways provided in your methods

• Although it is easier to declare fields as public, doing so is considered poor object-oriented programming design

• Data should always be hidden when possible and access should be controlled by well-designed methods

Page 19: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

19

Using Public Fields and Private Methods

• Poorly designed Desk class and program that instantiates a Desk

Page 20: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

20

Using Public Fields and Private Methods

• The Carpet class

Page 21: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

21

Using Public Fields and Private Methods

• Occasionally, there are instances where a data field should be declared as public

• A data field may be declared public if all objects of a class contain the same value for that data field

• The preceding example (fig 4-12) declared a public const field

• A named constant within a class is always static

Page 22: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

22

Understanding the this Reference

• When instances of a class are created, the resulting objects require separate memory locations in the computer

• In the following code, each object of Book would at least require separate memory locations for title, numPages, and Price

Page 23: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

23

Understanding the this Reference

• Unlike the class fields that are unique to each instance, the methods of the class are shared

• Although the methods are shared, there must be a difference between two method calls (from different objects), because each returns a different value (based on their unique fields)

• When a method is called, you automatically pass a reference to the memory of the object into the method

• The implicitly passed reference is the this reference

Page 24: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

24

Understanding the this Reference

• Book class methods explicitly using the this reference

Page 25: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

25

Understanding the this Reference

• Book class method that requires explicit this reference

Page 26: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

26

Understanding Constructor Methods

• A constructor method is a method that establishes an object

• Every class created is automatically supplied with a public constructor method with no parameters

• Any constructor method you write must have the same name as its class, and constructor methods cannot have a return type

Page 27: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

27

Passing Parameters to Constructors

• You can create a constructor that sets the same value for all objects instantiated. The program can eventually call the method of the object to set the value. However, this might not always be the most efficient technique.

Page 28: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

28

Passing Parameters to Constructors

• In this code, the constructor receives an argument and initializes the object with the appropriate information

Page 29: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

29

Overloading Constructors

• C# automatically provides a default constructor• However if you create your own constructor, the

automatically provided default constructor is not available

• C# constructor methods, like other methods, can be overloaded

Page 30: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

30

Understanding Destructor Methods

• A destructor method contains the actions you require when an instance of a class is destroyed

• To explicitly declare a destructor method, use an identifier that consists of a tilde(~) followed by the class name

• Destructors cannot receive parameters and therefore cannot be overloaded

• A class can have at most one destructor

Page 31: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

31

Understanding Destructor Methods

• Employee class with destructor method

Page 32: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

32

Understanding Destructor Methods

• Destructor methods are automatically called when an object goes out of scope

• You cannot explicitly call a destructor• The last object created is the first object destroyed

Page 33: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

33

Chapter Summary

• When you write programs in C#, you create two distinct type of classes

• When you create a class, you create a class header and a class body

• You declare the attributes and instance variables for a class within the curly braces using the same syntax you use to declare other variables

• Declaring a class does not create any actual objects• After an object has been instantiated, its public methods

can be accessed using the object’s identifier, a dot, and a method call

Page 34: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

34

Chapter Summary

• When you create a class that describes objects you will instantiate, and another class that instantiates those objects, you physically can contain both classes within the same file or place each class in its own file

• Although there is no requirement to do so, most programmers place data fields in some logical order at the beginning of a class

• Most programmers make class data fields private and class methods public

Page 35: 1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn

35

Chapter Summary

• When you create an object, you provide storage for each of the object’s instance variables, but not for each instance method

• A constructor method establishes an object• You can create objects that hold unique fields right from

the start• Like any other C# methods, constructors can be

overloaded• A destructor method contains the actions you require

when an instance of a class is destroyed