Chapter 9: Classes and Objects

1. What is a Class? (Super Simple Analogy)

Think of a class like a blueprint or cookie cutter:

  • The class defines what something looks like and what it can do.
  • An object is an actual thing you create using that blueprint (like an actual cookie made from the cookie cutter).

Example:

  • Class = Car (blueprint: has color, model, speed, can drive, can honk…)
  • Object = myToyota, yourTesla, friend’sMaruti (actual cars made from the Car blueprint)

2. Defining a Simple Class

Let’s create our first class!

C#

How to use it (create objects):

C#

3. Fields vs Properties (Very Important Difference!)

Fields → private variables that store data (usually hidden)

Properties → public way to get and set fields (with control)

There are two main types of properties in 2026 C#:

A. Auto-implemented Properties (Most Common – Super Clean!)

C#

B. Full Properties (When you want control / validation)

C#

4. Constructors – How Objects Are Born

A constructor is a special method that runs automatically when you create an object with new.

A. Default Constructor (No Parameters)

C#

B. Parameterized Constructor (Most Useful)

C#

C. Static Constructor (Rare – Runs Once Per Class)

C#

5. Object Initialization Syntax (Modern & Very Clean – C# 3+)

You can set properties right after new without calling a constructor!

C#

6. Mini-Project: Bank Account Class

Let’s build something real!

C#

Summary – What We Learned Today

  • Class = blueprint
  • Object = instance created with new
  • Fields → private data
  • Properties → public access (auto or full)
  • Constructors → default, parameterized, static
  • Object initializer → { Property = value } syntax

Your Homework (Super Fun & Practical!)

  1. Create a new console project called OOPBasics
  2. Create a class called Car with:
    • Properties: Make, Model, Year, Color, CurrentSpeed (private set)
    • Constructor that takes Make, Model, Year, Color
    • Method Accelerate(int speedIncrease)
    • Method Brake(int speedDecrease)
    • Method ShowStatus() → “My Toyota Corolla (2023, Red) is going 80 km/h”
  3. In Program.cs, create 2–3 cars, accelerate them, brake one, and show their status

Next lesson: Methods in Depth – parameters, overloading, ref/out, optional parameters – we’re going to make objects do even more amazing things!

You’re doing absolutely fantastic! 🎉 Any part confusing? Want more examples with constructors or properties? Just tell me — I’m right here for you! 💙

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *