0% found this document useful (0 votes)
20 views2 pages

OOP Report English Formatted

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

OOP Report English Formatted

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

Object-Oriented Programming Report

Object-Oriented Programming (OOP)


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). The main
principles of OOP are encapsulation, inheritance, polymorphism, and abstraction.

1. Encapsulation
In C#, encapsulation allows bundling data and methods within classes. Access modifiers
such as `private`, `public`, `protected`, and `internal` control the visibility of properties and
methods, enabling encapsulation and restricting direct access to an object's data.

Example:

public class Car {


private string color;

public void SetColor(string color) {


this.color = color;
}

public string GetColor() {


return color;
}
}

2. Inheritance
Inheritance allows a new class to inherit attributes and methods from an existing class,
which reduces code redundancy and enables hierarchical classification.

Example:

public class Vehicle {


public int Speed { get; set; }
}

public class Car : Vehicle {


public string Model { get; set; }
}

3. Polymorphism
Polymorphism in C# allows objects of different classes to be treated as objects of a common
superclass. This can be achieved using virtual methods and method overriding.

Example:
public class Animal {
public virtual void Speak() {
Console.WriteLine("Animal sound");
}
}

public class Dog : Animal {


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

4. Abstraction
Abstraction in C# can be achieved using abstract classes and interfaces, which allow
defining methods without implementing them. This hides complex details and provides a
simplified interface.

Example:

public abstract class Shape {


public abstract double CalculateArea();
}

public class Circle : Shape {


public double Radius { get; set; }

public override double CalculateArea() {


return Math.PI * Radius * Radius;
}
}

You might also like