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 about instance variables and methods How to declare objects
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
4 Objectives How to write constructor methods How to pass parameters to constructors How to overload constructors How to write destructor methods
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”
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
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
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
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
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
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
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
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
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
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
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”
17 Organizing Your Classes In addition to organizing fields and methods, comments should also be used
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
19 Using Public Fields and Private Methods Poorly designed Desk class and program that instantiates a Desk
20 Using Public Fields and Private Methods The Carpet class
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
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
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
24 Understanding the this Reference Book class methods explicitly using the this reference
25 Understanding the this Reference Book class method that requires explicit this reference
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
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.
28 Passing Parameters to Constructors In this code, the constructor receives an argument and initializes the object with the appropriate information
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
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
31 Understanding Destructor Methods Employee class with destructor method
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
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
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
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