Oops
Oops
C#
• object-oriented programming is a strategy that will provide principles to work with the objects while developing the
applications.
• Object-oriented programming will allow users to create objects which are instances of classes and allow them to create
methods to operate on those objects.
• Generally, in any programming language object-oriented programming is commonly called OOPS.
•
• C# is a fully object-oriented programming language and it provides support for OOPS concepts.
• If we build applications using object-oriented programming it will help us to improve the code reusability and
maintainability.
•
• The following are some basic principles of object-oriented programming to build object-oriented applications.
• Classes and Objects in C#
• Abstraction in C#
• Encapsulation in C#
• Inheritance in C#
• Polymorphism in C#
Classes and Objects in C#
• Class is a user defined data type. it is like a template.
• class is a data structure and it is helpful to combine various types of
data members such as fields, properties, methods, events, etc. into a
single unit.
Non-derived class ✔️ ✔️ ❌ ✔️ ❌ ❌
(same assembly)
Derived class ✔️ ✔️ ✔️ ❌ ❌ ❌
(different assembly)
Non-derived class ✔️ ❌ ❌ ❌ ❌ ❌
(different assembly)
Private Modifier
• If you declare a field with a private access modifier, it can only be accessed within the same
class:
• class Car
• {
• private string model = “Maruti";
• class Program
• {
• static void Main(string[] args)
• {
• Car myObj = new Car();
• Console.WriteLine(myObj.model);
• }
• }
• Error: 'Car.model' is inaccessible due to its protection level
The field 'Car.model' is assigned but its value is never used
Public Modifier
• If you declare a field with a public access modifier, it is accessible for all classes:
• class Car
• {
• public string model = "Maruti";
• }
• class Program
• {
• static void Main(string[] args)
• {
• Car myObj = new Car();
• Console.WriteLine(myObj.model);
• }
• }