0% found this document useful (0 votes)
5 views

C Sharp OOPS

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

C Sharp OOPS

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Real-Time Examples of OOP Principles in C#

Here are real-time examples that demonstrate the core Object-Oriented Programming (OOP) principles in C#,
including Encapsulation, Abstraction, Inheritance, Polymorphism, Interfaces, and Abstract Classes.

1. Real-Time Examples of Encapsulation Principle in C#


Encapsulation refers to the bundling of data (variables) and methods (functions) that operate on the data within a
single unit or class. It also restricts direct access to some of the object's components, which is why encapsulation is
often referred to as data hiding.
Example: Bank Account Class
public class BankAccount csharp
{
private decimal balance; // Private variable, cannot be accessed directly from out
side

// Public methods to access and modify the balance


public void Deposit(decimal amount)
{
if (amount > 0)
{
balance += amount;
}
}

public void Withdraw(decimal amount)


{
if (amount > 0 && balance >= amount)
{
balance -= amount;
}
}

public decimal GetBalance()


{
return balance;
}
}

public class Program


{
public static void Main(string[] args)
{
BankAccount account = new BankAccount();
account.Deposit(500);
account.Withdraw(200);
Console.WriteLine("Account Balance: " + account.GetBalance()); // 300
}
}

Explanation: The balance variable is encapsulated inside the BankAccount class and cannot be accessed
directly. Access is controlled via methods Deposit , Withdraw , and GetBalance .

2. Real-Time Examples of Abstraction Principle in C#


Abstraction is the concept of hiding the complex implementation details and exposing only the necessary parts of an
object. It allows the user to focus on what an object does instead of how it does it.
Example: Abstract Class for Shape
public abstract class Shape csharp
{
// Abstract method (does not have a body)
public abstract double Area();

// Regular method
public void Display()
{
Console.WriteLine("Displaying Shape...");
}
}

public class Circle : Shape


{
public double Radius { get; set; }

public override double Area()


{
return Math.PI * Radius * Radius;
}
}

public class Rectangle : Shape


{
public double Length { get; set; }
public double Width { get; set; }

public override double Area()


{
return Length * Width;
}
}

public class Program


{
public static void Main(string[] args)
{
Shape shape1 = new Circle { Radius = 5 };
Shape shape2 = new Rectangle { Length = 4, Width = 6 };

Console.WriteLine("Circle Area: " + shape1.Area()); // Abstract method


Console.WriteLine("Rectangle Area: " + shape2.Area()); // Abstract method
}
}

Explanation: The Shape class is abstract and does not define how to calculate the area. The Circle and
Rectangle classes implement the Area() method, abstracting away the complexity of how the area is
computed.

3. Real-Time Examples of Inheritance Principle in C#


Inheritance allows a class to inherit methods and properties from another class, enabling code reuse and the creation
of a hierarchical relationship between classes.
Example: Inheritance with Animals
public class Animal csharp
{
public string Name { get; set; }

public void Eat()


{
Console.WriteLine("Eating...");
}

public void Sleep()


{
Console.WriteLine("Sleeping...");
}
}

public class Dog : Animal


{
public void Bark()
{
Console.WriteLine("Barking...");
}
}

public class Cat : Animal


{
public void Meow()
{
Console.WriteLine("Meowing...");
}
}

public class Program


{
public static void Main(string[] args)
{
Dog dog = new Dog();
dog.Name = "Buddy";
dog.Eat(); // Inherited method
dog.Bark(); // Dog-specific method

Cat cat = new Cat();


cat.Name = "Whiskers";
cat.Sleep(); // Inherited method
cat.Meow(); // Cat-specific method
}
}

Explanation: The Dog and Cat classes inherit from the Animal class, so they share common methods like
Eat() and Sleep() , but each can have their own unique methods like Bark() and Meow() .

4. Real-Time Examples of Polymorphism Principle in C#


Polymorphism allows objects of different types to be treated as instances of the same base type. The two types of
polymorphism are method overriding (runtime polymorphism) and method overloading (compile-time polymorphism).
Example: Polymorphism with Animals
public class Animal csharp
{
public virtual void MakeSound()
{
Console.WriteLine("Some animal sound");
}
}

public class Dog : Animal


{
public override void MakeSound()
{
Console.WriteLine("Woof!");
}
}

public class Cat : Animal


{
public override void MakeSound()
{
Console.WriteLine("Meow!");
}
}

public class Program


{
public static void Main(string[] args)
{
Animal myDog = new Dog();
Animal myCat = new Cat();

myDog.MakeSound(); // Output: Woof!


myCat.MakeSound(); // Output: Meow!
}
}

Explanation: The MakeSound() method is overridden in the Dog and Cat classes. At runtime, the
appropriate MakeSound() method is called based on the object type ( Dog or Cat ), demonstrating
polymorphism.

5. Real-Time Examples of Interface in C#


Interface defines a contract that classes must adhere to. It provides a way to achieve multiple inheritance in C#, as a
class can implement multiple interfaces.
Example: Interface for Printable Entities
public interface IPrintable csharp
{
void Print();
}

public class Document : IPrintable


{
public string Content { get; set; }

public void Print()


{
Console.WriteLine("Printing document: " + Content);
}
}

public class Image : IPrintable


{
public string FileName { get; set; }

public void Print()


{
Console.WriteLine("Printing image: " + FileName);
}
}

public class Program


{
public static void Main(string[] args)
{
IPrintable doc = new Document { Content = "C# Programming Guide" };
IPrintable img = new Image { FileName = "CSharp.png" };

doc.Print(); // Output: Printing document: C# Programming Guide


img.Print(); // Output: Printing image: CSharp.png
}
}

Explanation: Both Document and Image classes implement the IPrintable interface and provide their
own implementation for the Print() method. The interface ensures both classes adhere to the contract to
implement the Print() method.

6. Real-Time Examples of Abstract Class in C#


Abstract Class serves as a blueprint for other classes. It can contain abstract methods (methods without
implementation) and concrete methods (methods with implementation).
Example: Abstract Class for Shape
public abstract class Shape csharp
{
public abstract double Area(); // Abstract method (no implementation)

public void Display()


{
Console.WriteLine("Displaying Shape...");
}
}

public class Circle : Shape


{
public double Radius { get; set; }

public override double Area()


{
return Math.PI * Radius * Radius;
}
}

public class Square : Shape


{
public double Side { get; set; }

public override double Area()


{
return Side * Side;
}
}
public class Program
{
public static void Main(string[] args)
{
Shape circle = new Circle { Radius = 5 };
Shape square = new Square { Side = 4 };

Console.WriteLine("Circle Area: " + circle.Area()); // Output: Circle Area: 7


8.54
Console.WriteLine("Square Area: " + square.Area()); // Output: Square Area: 16
}
}

Explanation: The Shape class is abstract and contains an abstract method Area() , which is implemented
by the Circle and Square classes. This allows us to define common behavior (like Display() ) while
leaving the implementation of Area() to the derived classes.

Summary of Principles:
Encapsulation: Hides the internal state of objects and exposes only the necessary functionality (e.g.,
BankAccount ).

Abstraction: Hides implementation details and exposes essential features (e.g., Shape with abstract methods).
Inheritance: Promotes code reuse by allowing a class to inherit properties and methods from another
(e.g., Animal class).
Polymorphism: Enables methods to work with objects of different types (e.g., MakeSound() in different
animals).
Interface: Defines a contract that classes must follow (e.g., IPrintable interface).
Abstract Class: Provides a base class with both abstract and concrete methods (e.g., Shape with Area() ).
These examples showcase how fundamental OOP concepts are applied in real-world C# programming to improve
code organization, maintainability, and scalability.

You might also like