Object-Oriented Programming (OOP) Basics in C#
Object-Oriented Programming (OOP) Basics
Introduction to OOP
Object-Oriented Programming (OOP) is a programming paradigm that organizes code into reusable and interconnected units called objects. These objects represent real-world entities or concepts, making it easier to design, build, and maintain complex software systems. OOP is widely used in software development due to its ability to model real-world scenarios, promote code reuse, and improve code readability.
OOP is built on four key principles:
Encapsulation: Bundling data (fields) and behavior (methods) into a single unit (class) and restricting access to internal details.
Inheritance: Allowing a class (child) to inherit properties and behavior from another class (parent).
Polymorphism: Enabling a single function or method to behave differently based on the context.
Abstraction: Hiding complex details and showing only the essential features of an object.
Classes and Objects
In C#, a class is a blueprint for creating objects, while an object is an instance of a class.
Syntax to Define and Instantiate a Class
// Define a class
public class Person
{
public string Name;
public int Age;
}
// Create an object
Person person1 = new Person();
person1.Name = "Alice";
person1.Age = 25;
Example
Let’s create a class to represent a car:
public class Car
{
public string Brand;
public string Model;
public int Year;
}
Car car1 = new Car();
car1.Brand = "Toyota";
car1.Model = "Corolla";
car1.Year = 2020;
Fields and Properties
Fields
Fields store data for an object. They are variables defined within a class.
public class Animal
{
public string Name;
public int Age;
}
Properties
Properties control access to fields, allowing validation or additional logic using get and set accessors.
public class Animal
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
Animal animal = new Animal();
animal.Name = "Elephant";
Console.WriteLine(animal.Name); // Outputs: Elephant
Constructors
A constructor is a special method used to initialize objects when they are created.
Default Constructor
public class Book
{
public string Title;
public Book()
{
Title = "Untitled";
}
}
Book book1 = new Book();
Console.WriteLine(book1.Title); // Outputs: Untitled
Parameterized Constructor
public class Book
{
public string Title;
public Book(string title)
{
Title = title;
}
}
Book book2 = new Book("C# for Beginners");
Console.WriteLine(book2.Title); // Outputs: C# for Beginners
Encapsulation and Access Modifiers
Encapsulation
Encapsulation restricts direct access to object data by using private fields and exposing them through public properties.
public class BankAccount
{
private double balance;
public double Balance
{
get { return balance; }
set
{
if (value >= 0)
balance = value;
}
}
}
BankAccount account = new BankAccount();
account.Balance = 100;
Console.WriteLine(account.Balance); // Outputs: 100
Access Modifiers
Example:
public class Example
{
private int privateField;
public int PublicField;
}
By practicing these exercises, you will strengthen your understanding of OOP principles in C# and become more confident in applying them to real-world problems.
Citation and Reference:-
Automation QA Engineer | Cypress.io | Robot Framework | Playwright | JavaScript | Cucumber BDD | Blockchain & AI Enthusiast
2 个月Useful tips