0% found this document useful (0 votes)
8 views20 pages

Oops

Uploaded by

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

Oops

Uploaded by

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

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.

• Public class Employees {

// Properties, fields, methods, etc.


}
• Object is run time entity which has different attribute to identify it
uniquely.
• object is an instance of a class, and it is useful to access the class
members and member functions.
• example of creating the object of the Employees class.
• Employees emp = new Employees();
• By using the emp object, we can access all the Employees class data
members and member functions.
Abstraction
• Abstraction shows only the essential information and hides the non-
essential details.
• abstraction is one of the basic principles of object-oriented
programming.
• The abstraction is useful to hide the implementation details and
expose only the essential features of an object.
• In c#, we can implement abstraction functionality by using
abstract classes and interfaces.
• Abstraction is needed when we only inherit from a certain class but
do not need to instantiate objects of that class
Encapsulation
• encapsulation is a process of binding data members, member
functions into a single unit.
• By using encapsulation, you can restrict the access of particular
members of the class to the outside and allow access to the specific
members of an object based on your requirements.
• The purpose of encapsulation is to prevent alteration of data from
outside.
• This data can only be accessed by getter functions of the class.
• As in encapsulation, the data in a class is hidden from other classes,
so it is also known as data-hiding.
• Encapsulation can be achieved by: Declaring all the variables in the
class as private and using C# Properties in the class to set and get the
values of variables
// C# program to illustrate encapsulation
• using System;
• public class DemoEncap {
• private String studentName;
• private int studentAge;
• public String Name
• {
• get { return studentName; }
• set { studentName = value; }
• }
• public int Age
• {
• get { return studentAge; }
• set { studentAge = value; }
• }
• }
• class Program {
• static public void Main()
• {
• DemoEncap obj = new DemoEncap();
• obj.Name = "Ankita”;

• // calls set accessor of the property Age,
• obj.Age = 21;

• // Displaying values of the variables


• Console.WriteLine(" Name : " + obj.Name);
• Console.WriteLine(" Age : " + obj.Age);
• }
• }
What are Abstraction and
Encapsulation?
• The process of representing the • The process of binding the data
essential features without and functions together into a
including the background details single unit (i.e. class) is called
is called Abstraction. Encapsulation.
• Abstraction and Encapsulation are related to each other. We can say
that Abstraction is logical thinking whereas Encapsulation is its
physical implementation.
control the accessibility of class
members using the following
access modifiers.
• Public
• Private
• Protected
• Internal
• Protected Internal
Modifier Description
public The code is accessible for all classes
private The code is only accessible within the same class
protected The code is accessible within the same class, or in a class that is
inherited from that class.
internal The code is only accessible within its own assembly, but not
from another assembly.
Caller's location public protected protected internal private private
internal protected

Within the class ✔️ ✔️ ✔️ ✔️ ✔️ ✔️

Derived class (same ✔️ ✔️ ✔️ ✔️ ✔️ ❌


assembly)

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";

• static void Main(string[] args)


• {
• Car myObj = new Car();
• Console.WriteLine(myObj.model);
• }
• }
• 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);
• }
• }

You might also like