Classes and Objects
Imagine you have a box. This box is special because it can hold different things like toys, books, or anything you like. In programming, we have something similar called a “class.”
1. What is a Class?
A class is like a blueprint or plan for creating objects. Imagine you have a blueprint for building a house. It tells you what the house should look like and what rooms it should have. Similarly, a class tells the computer what properties and actions an object should have when it’s created.
Structure of Class
// Define a class
[access_modifier] class ClassName
{
//Execution code like methods, fields , properties, constructor etc
}
2. What is an Object?
Now, imagine using that blueprint to create an actual car. That car you create is called an “object.”
An object is a real instance or copy of a class. It has unique properties (like the colour of the car) and behaviours (like driving or stopping).
Structure of Object
// Creating an object
ClassName objectName = new ClassName(parameters);// Accessing fields
objectName.fieldName = value;
// Calling methods
objectName.MethodName();
Example: Car Class and Objects
using System;
class Car
{
public string Colour;
public int Speed;
public void Drive(string colour , int speed)
{
Console.WriteLine(“The “+ colour +” car driving speed is ” + speed);
}
static void Main()
{
Car FirstCar = new Car();
FirstCar.Colour = “Red”;
FirstCar.Speed = 60;
Car SecondCar = new Car();
SecondCar.Colour = “Blue”;
SecondCar.Speed = 200;
FirstCar.Drive(FirstCar.Colour, FirstCar.Speed);
SecondCar.Drive(SecondCar.Colour,SecondCar.Speed);
}
}
Explanation
using System;
Define Simple class for the Car
class Car
Properties (attributes) of the car
public string Colour;
public int Speed;
Methods (actions) the car can do
public void Drive(string colour , int speed)
{
Console.WriteLine(“The “+ colour +” car driving speed is ” + speed);
}
static void Main()
{
Let’s create objects (instances) of the Car class:
Car FirstCar = new Car();
Creating a new car object
FirstCar.Colour = “Red”;
Setting the colour of the car
FirstCar.Speed = 60;
Setting the speed of the car
Car SecondCar = new Car();
Creating another car object to create second car
SecondCar.Colour = “Blue”;
Setting colour
SecondCar.Speed = 200;
Setting speed
Using the method of the car objects
FirstCar.Drive(FirstCar.Colour, FirstCar.Speed);
Output: The Red car driving speed is 60
SecondCar.Drive(SecondCar.Colour,SecondCar.Speed);
Output: The Blue car driving speed is 200
Course Video
Course Video In English
Task:
1. Create a class named Employee with the following properties:
string Name
int Id
double Salary
Create an object of the Employee class in the main method.
Assign values to the properties and print them to the console.
2. Create a class named Product with the following properties:
string ProductName
string Category
double Price
Create an object of the Product class in the main method.
Assign values to the properties and print them to the console.
3. Create a class named Student with the following properties:
string Name
int RollNumber
string Course
Create an object of the Student class in the main method.
Assign values to the properties and print them to the console.
4. Create a class named Car with the following properties:
string Make
string Model
int Year
Create an object of the Car class in the main method.
Assign values to the properties and print them to the console.
5. Create a class named Book with the following properties:
string Title
string Author
int Pages
Create an object of the Book class in the main method.
Assign values to the properties and print them to the console
An object is an instance of a class in C#. It represents a real-world entity and can contain data (fields) and actions (methods) based on the class blueprint.
A class is a blueprint for creating objects. It defines properties, methods, events, and other members that provide functionality to objects.
Objects in C# are created using the new keyword. For example:
ClassName obj = new ClassName();
A class acts as a template, while objects are concrete instances of that template. You can create multiple objects from a single class.
Yes, a single class can be used to create multiple objects, each having its own separate state and behavior.
Static classes in C# cannot be instantiated. They are used to group related methods or properties that do not require an object to operate.
Structs are value types, meaning they store data directly, whereas classes are reference types and store references to the data. Structs are better for small, lightweight objects.
Inheritance allows a class (child) to inherit members from another class (parent), promoting code reuse and hierarchical class structures.
Constructors initialize an object’s state when it is created. They set default values or execute necessary startup procedures.
Abstract classes cannot be instantiated and are used as a base class. They can include both implemented methods and abstract methods (without implementation).
You can learn classes and objects in C# for free through online courses at Iqra Academy, which include detailed explanations and examples for beginners.
Yes, Iqra Academy offers free video tutorials in Hindi that explain classes and objects in C# with step-by-step examples.
A class in C# typically contains:
- Fields: Variables to store data.
- Properties: Encapsulated fields with getter and setter methods.
- Methods: Functions that define the behavior of the class.
- Constructors: Special methods for initializing objects.
Classes and objects are core concepts in object-oriented programming (OOP). They enable modular, reusable, and organized code for building complex applications.
Yes, free online tutorials at Iqra Academy provide practical examples of how to define and use classes and objects in C#.
You can practice by writing small programs to create classes, define properties and methods, and instantiate objects using free online editors like dotnetfiddle.net.