DevAcademia
C++C#CPythonJava
  • C# Basics

  • C# Introduction
  • C# Get Started
  • C# Syntax
  • C# Output
  • C# Comments
  • C# Variables
  • C# Data Types
  • C# Type Casting
  • C# User Input
  • C# Operators
  • C# Math
  • C# Strings
  • C# Booleans
  • C# If...Else
  • C# Switch Statement
  • C# While Loop
  • C# For Loop
  • C# Break and Continue
  • C# Arrays
  • C# Files
  • C# OOP

  • C# OOP Introduction
  • C# Classes and Objects
  • C# Class Members
  • C# Constructors
  • C# Destructors
  • C# Access Modifiers
  • C# Properties
  • C# Inheritance
  • C# Polymorphism
  • C# Abstraction
  • C# Interfaces
  • C# Enums
  • C# Exceptions
  • C# Quizzes

  • C# Quiz Introduction
  • C# Basics

  • C# Introduction
  • C# Get Started
  • C# Syntax
  • C# Output
  • C# Comments
  • C# Variables
  • C# Data Types
  • C# Type Casting
  • C# User Input
  • C# Operators
  • C# Math
  • C# Strings
  • C# Booleans
  • C# If...Else
  • C# Switch Statement
  • C# While Loop
  • C# For Loop
  • C# Break and Continue
  • C# Arrays
  • C# Files
  • C# OOP

  • C# OOP Introduction
  • C# Classes and Objects
  • C# Class Members
  • C# Constructors
  • C# Destructors
  • C# Access Modifiers
  • C# Properties
  • C# Inheritance
  • C# Polymorphism
  • C# Abstraction
  • C# Interfaces
  • C# Enums
  • C# Exceptions
  • C# Quizzes

  • C# Quiz Introduction

Loading Cs tutorial…

Loading content
C# OOPTopic 39 of 55
←PreviousPrevNextNext→

C# Object-Oriented Programming (OOP)

Introduction to OOP

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects that combine state and behavior.

In C#, OOP allows developers to model real-world entities as classes and objects, which have properties (data) and methods (functions).

The four main principles of OOP are: Encapsulation, Inheritance, Polymorphism, and Abstraction.

Classes and Objects

A class defines a blueprint for creating objects. It can contain fields, properties, and methods.

An object is an instance of a class. Multiple objects can be created from the same class, each with its own state.

Example
using System;

namespace OOPExample
{
    class Car
    {
        public string Brand;
        public int Year;

        public void Drive()
        {
            Console.WriteLine($"{Brand} is driving.");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Car myCar = new Car();
            myCar.Brand = "BMW";
            myCar.Year = 2022;

            Console.WriteLine($"Car: {myCar.Brand}, Year: {myCar.Year}");
            myCar.Drive();
        }
    }
}
Output
Car: BMW, Year: 2022
BMW is driving.

Encapsulation

Encapsulation restricts direct access to an object's data and enforces controlled access through properties or methods.

Access modifiers like public, private, and protected are used to control visibility, and properties with getters and setters provide a safe way to expose data.

Example
using System;

namespace EncapsulationExample
{
    class Person
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Person p = new Person();
            p.Name = "Alice";
            Console.WriteLine("Name: " + p.Name);
        }
    }
}
Output
Name: Alice

Inheritance

Inheritance allows one class (derived class) to reuse members of another class (base class).

In C#, the ':' symbol is used to declare inheritance.

Example
using System;

namespace InheritanceExample
{
    class Animal
    {
        public void Eat()
        {
            Console.WriteLine("This animal eats food.");
        }
    }

    class Dog : Animal
    {
        public void Bark()
        {
            Console.WriteLine("The dog barks.");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Dog d = new Dog();
            d.Eat();
            d.Bark();
        }
    }
}
Output
This animal eats food.
The dog barks.

Polymorphism

Polymorphism allows methods in a base class to be redefined in derived classes, giving different behavior for the same method call.

In C#, this is commonly achieved with the 'virtual' keyword in the base class and 'override' in the derived class.

Example
using System;

namespace PolymorphismExample
{
    class Animal
    {
        public virtual void Speak()
        {
            Console.WriteLine("The animal makes a sound.");
        }
    }

    class Dog : Animal
    {
        public override void Speak()
        {
            Console.WriteLine("The dog barks.");
        }
    }

    class Cat : Animal
    {
        public override void Speak()
        {
            Console.WriteLine("The cat meows.");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Animal a1 = new Dog();
            Animal a2 = new Cat();

            a1.Speak();
            a2.Speak();
        }
    }
}
Output
The dog barks.
The cat meows.

Abstraction

Abstraction focuses on hiding implementation details and exposing only the necessary operations to the user.

In C#, abstraction can be achieved using abstract classes or interfaces.

Example
using System;

namespace AbstractionExample
{
    abstract class Shape
    {
        public abstract void Draw();
    }

    class Circle : Shape
    {
        public override void Draw()
        {
            Console.WriteLine("Drawing a Circle.");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Shape s = new Circle();
            s.Draw();
        }
    }
}
Output
Drawing a Circle.
Test your knowledge: C# Object-Oriented Programming (OOP)
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
C# OOPTopic 39 of 55
←PreviousPrevNextNext→