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

Lect 6 - Object-Oriented Programming (OOP) in C# - Copy

Oop

Uploaded by

xxareefa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lect 6 - Object-Oriented Programming (OOP) in C# - Copy

Oop

Uploaded by

xxareefa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Visual Programming

Visual Programming

Object-Oriented Programming (OOP) in C#

Fundamental Programming Paradigm

By:
Zohair Ahmed

www.youtube.com/@zohairahmed007

www.youtube.com/@zohairahmed007 www.youtube.com/@zohairahmed007
Subscribe
www.begindiscovery.com

By : Zohair Ahmed www.youtube.com/@zohairahmed007


www.begindiscovery.com

www.begindiscovery.com www.begindiscovery.com
Object-Oriented Programming (OOP)
• Object-Oriented Programming (OOP) in C# is a fundamental programming paradigm that
structures code into "objects," which represent real-world entities.

• OOP in C# involves four core principles: Encapsulation, Abstraction, Inheritance, and


Polymorphism.

www.youtube.com/@zohairahmed007

www.begindiscovery.com

By : Zohair Ahmed www.youtube.com/@zohairahmed007 www.begindiscovery.com


Encapsulation public class Person
{
// Private fields
• Encapsulation is the practice of bundling private string name;
private int age;
the data (fields) and methods that
// Public property with encapsulated field
operate on the data within one unit public string Name
{
(class) and restricting access to some of get { return name; }
set { name = value; }
the object's components. }

• This is typically achieved using access public int Age


{
modifiers like private, public, protected, get { return age; }
set
etc. {
if (value > 0) // Validation
age = value;
}
}
}
www.youtube.com/@zohairahmed007

www.begindiscovery.com

By : Zohair Ahmed www.youtube.com/@zohairahmed007 www.begindiscovery.com


Abstraction
public abstract class Animal
• Abstraction is the concept of hiding {
complex implementation details and // Abstract method with no implementation
exposing only the essential features of an
public abstract void Speak();
}
object.

• In C#, abstraction is achieved through public class Dog : Animal


{
abstract classes and interfaces.
public override void Speak()
{
Console.WriteLine("Woof!");
}
}

www.youtube.com/@zohairahmed007

www.begindiscovery.com

By : Zohair Ahmed www.youtube.com/@zohairahmed007 www.begindiscovery.com


Inheritance
public class Vehicle
• Inheritance allows a class to inherit the {
properties and methods of another class. public string Brand = "Ford";
public void Honk()
• In C#, inheritance is achieved using a :
{
symbol. Console.WriteLine("Beep beep!");
}
}

// Car inherits from Vehicle


public class Car : Vehicle
{
public string ModelName = "Mustang";
}
www.youtube.com/@zohairahmed007

www.begindiscovery.com

By : Zohair Ahmed www.youtube.com/@zohairahmed007 www.begindiscovery.com


Type of Inheritance
• Single Inheritance
‒ Single inheritance occurs when a class inherits from one and only one base class. This is the
most common type of inheritance.
• Multilevel Inheritance
‒ In multilevel inheritance, a class is derived from a class that is also derived from another class. This
creates a hierarchy of classes.
• Hierarchical Inheritance
‒ Hierarchical inheritance occurs when multiple classes inherit from a single base class.
• Multiple Inheritance (Through Interfaces)
‒ C# does not support multiple inheritance directly (a class cannot inherit from more than one class), but
it can be achieved using interfaces. A class can implement multiple interfaces, thus achieving a form
of multiple inheritance.
www.youtube.com/@zohairahmed007

www.begindiscovery.com

By : Zohair Ahmed www.youtube.com/@zohairahmed007 www.begindiscovery.com


Example: Type of Inheritance
//1. Single Inheritance //3. Hierarchical Inheritance
public class Parent
public class Parent {
{ public void Display() { Console.WriteLine("Parent Class"); }
public void Display() { Console.WriteLine("Parent Class"); } }
} public class Child1 : Parent
{
public class Child : Parent public void Show() { Console.WriteLine("Child1 Class"); }
{ }
public void Show() { Console.WriteLine("Child Class"); } public class Child2 : Parent
} {
public void Print() { Console.WriteLine("Child2 Class"); }
//2. Multilevel Inheritance }
public class Grandparent //4. Multiple Inheritance (Through Interfaces)
{ public interface IA
public void Display() { Console.WriteLine("Grandparent Class"); } {
} void MethodA();
}
public class Parent : Grandparent public interface IB
{ {
public void Show() { Console.WriteLine("Parent Class"); } void MethodB();
} }
public class Child : IA, IB
public class Child : Parent {
{ public void MethodA() { Console.WriteLine("Method from Interface IA"); }
public void Print() { Console.WriteLine("Child Class"); } public void MethodB() { Console.WriteLine("Method from Interface IB"); }
} }
www.youtube.com/@zohairahmed007

www.begindiscovery.com

By : Zohair Ahmed www.youtube.com/@zohairahmed007 www.begindiscovery.com


Polymorphism Method Overloading:
public class MathOperations
• Polymorphism allows objects to be {
public int Add(int a, int b) { return a + b; }
treated as instances of their parent class. public double Add(double a, double b) { return a + b; }
}
• C# supports two types of polymorphism:
Method Overriding
‒ Compile-time (method overloading) public class Animal
‒ Runtime (method overriding). {
public virtual void Speak() { Console.WriteLine("Animal
sound"); }
}

public class Cat : Animal


{
public override void Speak() {
Console.WriteLine("Meow"); }
www.youtube.com/@zohairahmed007
}
www.begindiscovery.com

By : Zohair Ahmed www.youtube.com/@zohairahmed007 www.begindiscovery.com


Example of Using OOP Concepts in C#
• In this example: public class Program
{
• Person demonstrates Encapsulation by public static void Main()
controlling access to its properties. {
Person person = new Person();
• Animal and Dog classes showcase person.Name = "Alice";
Abstraction with an abstract Speak person.Age = 25;

method. Dog dog = new Dog();


dog.Speak(); // Outputs: Woof!
• Vehicle and Car classes represent
Inheritance. Car myCar = new Car();
Console.WriteLine(myCar.Brand + " " + myCar.ModelName);
• Polymorphism is illustrated in Animal and myCar.Honk(); // Outputs: Beep beep!
Cat classes with method overriding. }
}
www.youtube.com/@zohairahmed007

www.begindiscovery.com

By : Zohair Ahmed www.youtube.com/@zohairahmed007 www.begindiscovery.com

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy