CSCI 1301

Classes & Objects

1. Learning Objectives

  • What is object-oriented programming (OOP)
  • What is a class
  • What is an object
  • How to declare a class in C#
  • How to instantiate an object in C#

2. What is Object-Oriented Programming

The remainder of this course is all about object-oriented programming. You may have not realized it, but you have actually been using an object-oriented language this entire semester, with the exception that we have been writing all code inside the Main method.

This strategy if fine for very small programs, but as we begin to write larger programs, it will be beneficial to learn how to organize our code into distinct classes. This style of organization makes the code reusable and easier to maintain.

Before we dive into the syntax, it is important to understand the basic ideas about what we are trying to accomplish when writing object-oriented programs.

I recommend you take the time to watch the following short video. There are 4 keywords I want you to pay special attention to: class, object, properties, methods. We will study these 4 topics in detail after the video.

3. Difference between Classes and Objects

Class

Similar to a blueprint of a house or a foundry mold; a class represents a model we use to create and an actual object.

  • Class is a definition of an entity
  • It represents a real-world object we want to model in a computer program
  • Class defines the properties and methods of the entity being modeled
  • Properties and method are collectively referred to as class members

Object

Objects are created from classes.

  • We say "object is an instance of a class", meaning we make a concrete realization of the class that was defined earlier
  • You can make multiple objects from one class
  • The process of creating an object is called instantiation

Visual Example

Consider this example,

img

  • In this example, a cookie cutter is analogous to a class. It is not a real cookie, but it defines what the cookie will be like. Just by observing this class gives us an idea of what the objects will be like.
  • The cookies on the right are analogous to objects. Notice the cookies are all different color, but all cookies share the resemblance of the class they were created from.
  • Also notice how we only need 1 class to create multiple objects. We can create as many objects as we want from this one class.

4. Declaring Classes in C#

When writing object-oriented programs, the first thing you must define is the class. That is, you cannot instantiate an object until you have the class.

All C# programs must have at least one class, but we are free to create infinitely more. Real-world programs typically have hundreds or even thousands of classes.

Class name (identifier) should reflect the entity you are about to model.

For example, if I wanted to create a class to represent the cookies from the previous section, I would choose class name Cookie. If I wanted to model a car, I would use class name Car. Similarly, Dog, Cat, Restaurant, User, Student are all also good class names for representing such entities.

The identifier you choose should be singular. It is conventional to capitalize the first letter of every word of the class name; this is a capitalization-style called PascalCase.

Class Declaration Syntax

Classes are declared by using class keyword followed by a unique identifier

An access modifier typically precedes class declaration, usually either public or private. We will learn more about access modifiers soon, but for now you should use public keyword when declaring classes. Access modifier is optional. If access modifier is omitted, default access level for a class is internal. For the purposes of this course internal works similarly to public access modifier.

The remainder of the class definition represents the class body. Use curly brackets { ... } to define the beginning and end of class body.

MyFirstClass.cs
1// SYNTAX
2// public - access modifier
3// class - keyword
4// identifier - appropriate class name
5
6public class MyFirstClass{
7
8 // class implementation will go here...
9
10}

5. How to Add Class Files to a C# Program

It is good practice to create 1 class / 1 file. The file name should match the class name. It will be easier to to find files later if you follow this principle.

However, in C# there is nothing preventing you from defining multiple classes within one .cs file, so you may see this occurring sometimes. Other object-oriented languages, like Java, are very strict about this 1 class / 1 file policy.

You should practice how to do this with your IDE:

  1. Create a new program (solution)
  2. Add a new class called MyFirstClass.cs
  3. Inside that MyFirstClass.cs, declare a class called MyFirstClass.

How to add new class in Visual Studio IDE

This short video shows how to add a class to a project using Visual Studio IDE

How to add new class using repl.it web IDE

Using the web IDE, you will find a button with "add file" that allows you to create a new class file.

img

6. Instantiating Objects in C#

Hopefully at this point you have created a class called MyFirstClass. If you did not create a class yet, go back and repeat the previous two instructional steps.

At this point our class is very basic. It is just an empty class, no methods or properties. But even with such a simple class, we have enough where we can go ahead and instantiate an object of this class.

Object Instantiation

To create an object variable, start with the name of the class you want to instantiate followed by a variable name: MyFirstClass csci (line 11).

Object is created by using new keyword followed by the name of the class you want to instantiate. You will want to assign the result of this operation to a variable of matching type. In the above example we assign the object to variable csci (line 11).

Once instantiated, an object is a block of memory that has been allocated and configured according to the blueprint of the class.

A program may create many objects of the same class, for example historyClass (lines 15-17) is a second object created from MyFirstClass.

You may create a variable for the object and then instantiate the object later in two separate commands. This behavior is demonstrated with historyClass (lines 15-17).

Be aware that class is a reference type. At run time, when you declare a variable of a reference type, the variable contains the value null until you explicitly create an instance of the class by using the new keyword, or assign it an object of a compatible type that may have been created elsewhere. In the example above, historyClass is null on line 15. After we assign an object to it on line 17 it is no longer null.


7. Summary

What is object-oriented programming (OOP)?

In very simple terms; object-oriented programming is a programming style where the programs are structured as classes. Classes are representations of real-world entities we want to model within the program. During execution we create instances of these classes; these instances are called objects.

What is a class?

Representation of a real-word entity within the program. A class has a name, and can include any number of properties and methods (including none). Similar to a blueprint, class represents an idea of something - not a concrete instance.

What is an object?

Objects are concrete instances of a class. Within a program, we use objects and interact with them. We can also instantiate multiple objects from one class.

How to declare a class in C#

This is the general syntax:

MyClass.cs
1public class MyClass {
2
3}

How to instantiate an object in C#

Main.cs
1// do this outside the class you want to instantiate
2// for example in the Main method
3
4MyClass obj1 = new MyClass();

8. Review Problems

View review problem for this topic