What is Object-Oriented Programming?
Object-Oriented Programming (OOPs) has revolutionized the world of software development. OOPs support modular, efficient, and scalable program architecture by organizing code into reusable objects. OOPs, have many applications, from desktop apps to online development. One of the primary benefits of OOPs is the ability to improve code reusability and maintainability. OOPs help developers design robust, adaptable, and easily expandable systems through encapsulation, inheritance, and polymorphism
Procedural programming is about writing methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods.
Usages of OOPs
- Modularity through Breakdown: By breaking complicated systems down into smaller, self-contained pieces, OOP excels at modularity, improving the reuse and maintainability of code.
- OOP facilitates DRY (“Don’t Repeat Yourself”) programming in C#, which makes the code simpler to maintain, modify, and debug.
- Effective Collaboration: The ability for numerous developers to work on specific components at the same time, thanks to modular architecture, increases productivity and accelerates development.
- Clearer Code Structure: Component-based approaches provide code that is understandable and easy to test.
- Data Encapsulation: OOP protects data and methods from outside intervention and fosters security by encapsulating them within objects
What is Class and Object?
Class is an example of a type of data structure. It contains Constants, and fields are examples of data members, and methods, properties, events, indexers, operators, instance constructors, finalizers, and static constructors are examples of function members.
- A “class” is a template that specifies the variables and methods of a certain type of object.
- The blueprint for a class is an object.
- Ex: In real life, a smartwatch offers features like weight and color, as well as modes like calling options, health monitoring, and games.
- Samsung Smart Watches and the iWatch are thus the objects of smartwatches.
public class SmartWatch
{
public WatchColor Color { get; set; }
public WatchShape Shape { get; set; }
public decimal Weight { get; set; }
public void Tracking()
{
Console.WriteLine("It has feature of Tracking");
}
public void Games()
{
Console.WriteLine("It has feature of Games");
}
}
SmartWatch iWatch=new SmartWatch();
iWatch.Tracking();
Here, SmartWatch is a class, and iWatch is an object for the SmartWatch.
The four pillars of object-oriented programming (AEIP)
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
Abstraction in C#
Abstraction is an important part of object-oriented programming. It means that only the required information is visible to the user, and the rest of the information is hidden.
Abstraction can be implemented using abstract classes in C#. Abstract classes are base classes with partial implementation. These classes contain abstract methods that are inherited by other classes that provide more functionality.
Some of the salient points about abstract classes are as follows.
- The abstract class is created using the keyword abstract, and some of the methods of the abstract class also contain the keyword abstract.
- No object can be created of the abstract class, i.e.it cannot be instantiated.
- The abstract methods in the abstract class are implemented only in the derived classes.
- If all the methods in the abstract class contain the keyword abstract, then that class is known as a pure Abstract class.
abstract class Shape
{
public abstract double Area();
}
class Circle : Shape
{
private readonly double radius;
public Circle(double r)
{
radius = r;
}
public override double Area()
{
return (3.14 * radius * radius);
}
}
class Square : Shape
{
private readonly double side;
public Square(double s)
{
side = s;
}
public override double Area()
{
return (side * side);
}
}
static void Main(string[] args)
{
Shape c = new Circle(5.0);
Console.WriteLine("Area of Circle = {0}", c.Area());
Shape s = new Square(2.5);
Console.WriteLine("Area of Square = {0}", s.Area());
Console.ReadKey();
}
Output::
----------------------
Area of Circle = 78.5
Area of Square = 6.25
The abstract class shape contains the abstract method area(). The code snippet for this is given as follows:
abstract class Shape
{
public abstract double Area();
}
The class Circle inherits from Shape. It has a private data member radius. The parameterized constructor assigns a value to the radius. The function area() calculates the area of the circle and returns that value. The code snippet for this is given as follows:
class Circle : Shape
{
private readonly double radius;
public Circle(double r)
{
radius = r;
}
public override double Area()
{
return (3.14 * radius * radius);
}
}

Join the conversation! Your thoughts help the community grow.