Introduction
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data in the form of fields (often known as attributes or properties) and code in the form of procedures (often known as methods). C# is a fully object-oriented programming language that provides robust features for building modular, reusable, and maintainable software.
Key Concepts of OOP
- Classes and Objects
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
1. Classes and Objects
Classes
A class is a blueprint for creating objects. It defines a type by bundling data and methods that work on the data into one single unit.
Syntax
public class ClassName
{
// Fields
private dataType fieldName;
// Properties
public dataType PropertyName { get; set; }
// Methods
public returnType MethodName(parameters)
{
// Method body
}
}
Example
using System;
namespace OOPExample
{
public class Car
{
// Fields
private string color;
private string model;
// Constructor
public Car(string color, string model)
{
this.color = color;
this.model = model;
}
// Properties
public string Color
{
get { return color; }
set { color = value; }
}
public string Model
{
get { return model; }
set { model = value; }
}
// Methods
public void StartEngine()
{
Console.WriteLine("Engine started.");
}
public void StopEngine()
{
Console.WriteLine("Engine stopped.");
}
}
}
Objects
An object is an instance of a class. It is created using the new
keyword.
Example
using System;
namespace OOPExample
{
class Program
{
static void Main(string[] args)
{
// Creating an object of the Car class
Car myCar = new Car("Red", "Toyota");
// Accessing properties
Console.WriteLine("Car Model: " + myCar.Model);
Console.WriteLine("Car Color: " + myCar.Color);
// Calling methods
myCar.StartEngine();
myCar.StopEngine();
}
}
}
Output
Car Model: Toyota
Car Color: Red
Engine started.
Engine stopped.
2. Encapsulation
Encapsulation is the process of wrapping code and data together into a single unit. In C#, encapsulation is implemented using access specifiers to control the accessibility of class members.
Example
public class Person
{
// Private fields
private string name;
private int age;
// Public properties
public string Name
{
get { return name; }
set { name = value; }
}
public int Age
{
get { return age; }
set
{
if (value > 0)
{
age = value;
}
}
}
// Constructor
public Person(string name, int age)
{
Name = name;
Age = age;
}
}
Example Usage
using System;
namespace EncapsulationExample
{
class Program
{
static void Main(string[] args)
{
Person person = new Person("Alice", 30);
Console.WriteLine("Name: " + person.Name);
Console.WriteLine("Age: " + person.Age);
person.Age = 35;
Console.WriteLine("Updated Age: " + person.Age);
}
}
}
Output
Name: Alice
Age: 30
Updated Age: 35
3. Inheritance
Inheritance is a mechanism by which one class (child class) can inherit the properties and methods of another class (parent class). It promotes code reusability.
Example
public class Animal
{
public void Eat()
{
Console.WriteLine("Eating...");
}
}
public class Dog : Animal
{
public void Bark()
{
Console.WriteLine("Barking...");
}
}
Example Usage
using System;
namespace InheritanceExample
{
class Program
{
static void Main(string[] args)
{
Dog dog = new Dog();
dog.Eat(); // Inherited method
dog.Bark(); // Child class method
}
}
}
Output
Eating...
Barking...
4. Polymorphism
Polymorphism allows methods to do different things based on the object it is acting upon. There are two types of polymorphism: compile-time (method overloading) and runtime (method overriding).
Method Overloading (Compile-Time Polymorphism)
public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
public double Add(double a, double b)
{
return a + b;
}
}
Example Usage
using System;
namespace PolymorphismExample
{
class Program
{
static void Main(string[] args)
{
Calculator calc = new Calculator();
Console.WriteLine("Sum of integers: " + calc.Add(5, 3));
Console.WriteLine("Sum of doubles: " + calc.Add(5.2, 3.3));
}
}
}
Output
Sum of integers: 8
Sum of doubles: 8.5
Method Overriding (Runtime Polymorphism)
public class Animal
{
public virtual void MakeSound()
{
Console.WriteLine("Animal sound");
}
}
public class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("Bark");
}
}
Example Usage
using System;
namespace MethodOverridingExample
{
class Program
{
static void Main(string[] args)
{
Animal myAnimal = new Dog();
myAnimal.MakeSound();
}
}
}
Output
Bark
5. Abstraction
Abstraction is the concept of hiding the complex implementation details and showing only the necessary features of an object. In C#, abstraction is achieved using abstract classes and interfaces.
Abstract Class
public abstract class Shape
{
public abstract double GetArea();
}
public class Circle : Shape
{
private double radius;
public Circle(double radius)
{
this.radius = radius;
}
public override double GetArea()
{
return Math.PI * radius * radius;
}
}
Example Usage
using System;
namespace AbstractionExample
{
class Program
{
static void Main(string[] args)
{
Shape shape = new Circle(5);
Console.WriteLine("Area of Circle: " + shape.GetArea());
}
}
}
Output
Area of Circle: 78.53981633974483
Interface
public interface IShape
{
double GetArea();
}
public class Rectangle : IShape
{
private double width;
private double height;
public Rectangle(double width, double height)
{
this.width = width;
this.height = height;
}
public double GetArea()
{
return width * height;
}
}
Example Usage
using System;
namespace InterfaceExample
{
class Program
{
static void Main(string[] args)
{
IShape rectangle = new Rectangle(4, 5);
Console.WriteLine("Area of Rectangle: " + rectangle.GetArea());
}
}
}
Output
Area of Rectangle: 20
Conclusion
Object-Oriented Programming (OOP) in C# is a powerful paradigm that helps in building modular, reusable, and maintainable software. By understanding and applying the key concepts of OOP?classes and objects, encapsulation, inheritance, polymorphism, and abstraction?you can create complex and efficient applications.