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 π₯
- Class Definition: We use the
class
keyword to define the class. - Properties: We define properties like
Make
,Model
, andYear
. These describe the car. - 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 π₯
- Creating an Object:
Car myCar = new Car();
creates a new instance of theCar
class and assigns it to themyCar
variable. - Setting Properties: We can access and modify the properties of the object, like
myCar.Make = "Toyota"
. - 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"
, and2020
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 isprivate
, so it can only be accessed inside theCar
class. - The
Model
property ispublic
, meaning it can be accessed from outside the class. - We provide public methods (
SetMake
andShowCarDetails
) 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 theMake
,Model
, andStartEngine()
method from theVehicle
class. Car
can also have its own unique methods, likeHonk()
.