Classes and Objects - potatoscript/csharp GitHub Wiki

πŸ₯” Classes and Objects in C# πŸ₯”

πŸ₯” What Are Classes and Objects? πŸ₯”

In C#, a class is like a blueprint or template that defines the properties (attributes) and behaviors (methods) of something. An object is an instance of a class β€” it is a real-life example of that blueprint.

Let’s think of a car as an example:

  • Class: A car blueprint that says a car has wheels, doors, and can drive.
  • Object: An actual car you see on the road. The car is made using the car blueprint, but each car (object) might have different attributes (color, brand, etc.).

πŸ₯” Creating a Class πŸ₯”

To create a class in C#, we define the class with a name and specify the properties and methods inside it. Let’s create a simple class called Car.

public class Car
{
    // Properties (Attributes)
    public string Make { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }

    // Method (Behavior)
    public void StartEngine()
    {
        Console.WriteLine("The engine is now running!");
    }
}

πŸ₯” Explanation πŸ₯”

  1. Class Definition: We use the class keyword to define the class.
  2. Properties: We define properties like Make, Model, and Year. These describe the car.
  3. Method: The StartEngine() method describes the behavior of the car (starting the engine).

πŸ₯” Creating an Object (Instance) πŸ₯”

To create an object, you use the new keyword to create an instance of the class. Let’s create an object of the Car class.

class Program
{
    static void Main()
    {
        // Creating an object (instance) of the Car class
        Car myCar = new Car();
        
        // Assigning values to the object's properties
        myCar.Make = "Toyota";
        myCar.Model = "Corolla";
        myCar.Year = 2020;

        // Calling the method on the object
        myCar.StartEngine();
        
        // Printing the car's details
        Console.WriteLine($"Car details: {myCar.Year} {myCar.Make} {myCar.Model}");
    }
}

πŸ₯” Explanation πŸ₯”

  1. Creating an Object: Car myCar = new Car(); creates a new instance of the Car class and assigns it to the myCar variable.
  2. Setting Properties: We can access and modify the properties of the object, like myCar.Make = "Toyota".
  3. Calling Methods: We call methods like myCar.StartEngine(); to make the car do something.

πŸ₯” Why Are Classes and Objects Useful? πŸ₯”

Classes and objects help organize your code in a way that reflects real-world things. Instead of writing everything in one big block of code, you can break it into smaller, reusable parts. Each part (class) is like a small unit with its own data and behavior.

  • Reusability: Once a class is written, you can create many objects from that class. For example, you could create several Car objects, each with its own make, model, and year.
  • Organization: Classes help organize your code into logical units, making it easier to maintain.

πŸ₯” Constructor Method πŸ₯”

Sometimes, you want to initialize an object with specific values when it’s created. This is where a constructor comes in.

A constructor is a special method in a class that gets called automatically when an object is created. It’s used to set the initial state of an object.

Here’s an example of a class with a constructor:

public class Car
{
    // Properties
    public string Make { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }

    // Constructor
    public Car(string make, string model, int year)
    {
        Make = make;
        Model = model;
        Year = year;
    }

    // Method
    public void StartEngine()
    {
        Console.WriteLine("The engine is now running!");
    }
}

Now, you can create an object of Car and provide values to the constructor right away:

class Program
{
    static void Main()
    {
        // Creating an object using the constructor
        Car myCar = new Car("Toyota", "Corolla", 2020);

        // Calling the method
        myCar.StartEngine();

        // Printing the car's details
        Console.WriteLine($"Car details: {myCar.Year} {myCar.Make} {myCar.Model}");
    }
}

πŸ₯” Explanation πŸ₯”

  • The constructor public Car(string make, string model, int year) is used to initialize the object with values when it's created.
  • When we create the myCar object, we pass the values "Toyota", "Corolla", and 2020 directly to the constructor, and these values are assigned to the properties.

πŸ₯” Access Modifiers πŸ₯”

In C#, we use access modifiers to control the visibility and accessibility of classes, properties, and methods. The most common access modifiers are:

  • public: The member is accessible from anywhere.
  • private: The member is only accessible within the same class.
  • protected: The member is accessible within the class and derived classes.

For example:

public class Car
{
    private string make;  // Private field, cannot be accessed outside the class
    public string Model { get; set; } // Public property, can be accessed from anywhere

    // Public method
    public void SetMake(string make)
    {
        this.make = make;  // Setting the private field value
    }

    // Public method
    public void ShowCarDetails()
    {
        Console.WriteLine($"Car Make: {make}, Model: {Model}");
    }
}

πŸ₯” Explanation πŸ₯”

  • The make field is private, so it can only be accessed inside the Car class.
  • The Model property is public, meaning it can be accessed from outside the class.
  • We provide public methods (SetMake and ShowCarDetails) to interact with the private data.

πŸ₯” Inheritance πŸ₯”

Inheritance is another key concept in object-oriented programming. It allows one class to inherit properties and methods from another class. The class that is inherited from is called the base class, and the class that inherits is called the derived class.

Here’s an example:

public class Vehicle
{
    public string Make { get; set; }
    public string Model { get; set; }

    public void StartEngine()
    {
        Console.WriteLine("The engine is running!");
    }
}

public class Car : Vehicle  // Car inherits from Vehicle
{
    public int Year { get; set; }

    public void Honk()
    {
        Console.WriteLine("Beep Beep!");
    }
}

Now, the Car class inherits all properties and methods from the Vehicle class:

class Program
{
    static void Main()
    {
        Car myCar = new Car();
        myCar.Make = "Toyota";
        myCar.Model = "Corolla";
        myCar.Year = 2020;

        myCar.StartEngine();  // Inherited method from Vehicle
        myCar.Honk();         // Method from Car class

        Console.WriteLine($"Car details: {myCar.Year} {myCar.Make} {myCar.Model}");
    }
}

πŸ₯” Explanation πŸ₯”

  • The Car class inherits the Make, Model, and StartEngine() method from the Vehicle class.
  • Car can also have its own unique methods, like Honk().