Introduction to Object-Oriented Programming in C#
1. What is Object-Oriented Programming (OOP)?
Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" to design applications and programs. It utilizes several principles including encapsulation, inheritance, and polymorphism.
2. Basic Principles of OOP
There are four fundamental principles of OOP:
- Encapsulation: Bundling the data (attributes) and methods (functions) that operate on the data into a single unit called a class.
- Inheritance: Mechanism by which one class can inherit the attributes and methods of another class.
- Polymorphism: Ability to present the same interface for different data types.
- Abstraction: Simplifying complex reality by modeling classes appropriate to the problem.
3. Creating a Class in C#
In C#, a class is created using the class keyword. Below is an example of a simple class:
public class Person { // Attributes public string Name; public int Age; // Method public void Greet() { Console.WriteLine("Hello, my name is " + Name); } }
In this example, Person
is a class with two attributes Name
and Age
, and one method Greet
.
4. Creating Objects
An object is an instance of a class. Here's how you can create an object of the Person
class:
Person person1 = new Person(); person1.Name = "John"; person1.Age = 30; person1.Greet(); // Output: Hello, my name is John
5. Encapsulation
Encapsulation is achieved by using access modifiers to protect the data within a class. The common access modifiers are public
, private
, protected
, and internal
. Let's modify our Person
class to use encapsulation:
public class Person { // Attributes private string name; private int age; // Properties public string Name { get { return name; } set { name = value; } } public int Age { get { return age; } set { age = value; } } // Method public void Greet() { Console.WriteLine("Hello, my name is " + Name); } }
6. Inheritance
Inheritance allows a class to inherit attributes and methods from another class. The class that inherits is called the derived class, and the class being inherited from is called the base class. Here's an example:
public class Person { public string Name { get; set; } public int Age { get; set; } public void Greet() { Console.WriteLine("Hello, my name is " + Name); } } public class Student : Person { public string School { get; set; } public void Study() { Console.WriteLine(Name + " is studying at " + School); } }
In this example, Student
inherits from Person
, so it has access to Name
, Age
, and Greet
method.
7. Polymorphism
Polymorphism allows methods to do different things based on the object it is acting upon. There are two types of polymorphism in C#: Compile-time (method overloading) and Run-time (method overriding). Here's an example of method overriding:
public class Person { public virtual void Greet() { Console.WriteLine("Hello!"); } } public class Student : Person { public override void Greet() { Console.WriteLine("Hello, I am a student."); } } public class Teacher : Person { public override void Greet() { Console.WriteLine("Hello, I am a teacher."); } }
In this example, both Student
and Teacher
override the Greet
method of the Person
class.
8. Abstraction
Abstraction is the concept of hiding the complex implementation details and showing only the essential features of the object. It is achieved using abstract classes and interfaces. Here's an example using an abstract class:
public abstract class Animal { public abstract void MakeSound(); public void Sleep() { Console.WriteLine("Sleeping..."); } } public class Dog : Animal { public override void MakeSound() { Console.WriteLine("Woof!"); } } public class Cat : Animal { public override void MakeSound() { Console.WriteLine("Meow!"); } }
In this example, Animal
is an abstract class that contains an abstract method MakeSound
and a concrete method Sleep
. The Dog
and Cat
classes implement the MakeSound
method.