OOPS

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Shoolini University

Yogananda School of AI, Computing & Data Science

C# Programming(CSU1531)
Section A
C# Language Basics and Advanced Features
 Classes and Objects
 Constructors

Outline  Access Modifiers


 Encapsulation
Everything in C# is associated with classes and objects, along with
its attributes and methods.

Since C# is an object-oriented language, program is designed using


objects and classes in C#.

The car has attributes, such as weight and color, and methods,
such as drive and brake.

Classes and Objects


Objects
Object
 In C#, Object is a real world entity, for example, car, mobile, laptop etc.

 Object is an entity that has state and behavior. Here, state means data and
behavior means functionality.

 Object is a runtime entity, it is created at runtime.

 Object is an instance of a class.

 All the members of the class can be accessed through object.

 Let's see an example to create object using new keyword.

Student s1 = new Student();


Class

In C#, class is a group of similar objects.

It is a template from which objects are created.

It can have fields, methods, constructors etc.


Example
using System;
public class Student {
int id; //data member (also instance variable)
String name;
//data member(also instance variable)
public static void Main(string[] args) {
Student s1 = new Student();
//creating an object of Student
s1.id = 101;
s1.name = "David";
Console.WriteLine(s1.id);
Console.WriteLine(s1.name);
}
}
Example 1
class Car
{
string color = "red";

static void Main(string[] args)


{
Car myObj = new Car();
Console.WriteLine(myObj.color);
}
}
Example 2
using System;
public class Student {
public int id;
public String name;
}
class TestStudent{
public static void Main(string[] args) {
Student s1 = new Student();
s1.id = 101;
s1.name = "David";
Console.WriteLine(s1.id);
Console.WriteLine(s1.name);
}
}
Example 3
class TestStudent{
using System;
public class Student { public static void
public int id; Main(string[] args) {
public String name; Student s1 = new
public void insert(int i, Stri Student();
ng n) { Student s2 = new
id = i; Student();
name = n;
s1.insert(101,
}
"Ajeet");
public void display() {
Console.WriteLine(id + " " s2.insert(102,
+ name); "Tom");
} s1.display();
}
s2.display();
}
}
Constructors in C#:
 A constructor is a special method that is used to initialize objects.
 Same Name as the Class: The constructor must have the same name as the
class.
 No Return Type: Unlike regular methods, constructors don’t have a return
type (not even void).
 Automatically Called: When you create a new object using new, the
constructor is automatically invoked.
 All classes have constructors by default: if you do not create a class
constructor yourself, C# creates one for you. However, then you are not
able to set initial values for fields.
 Can Be Overloaded: You can have multiple constructors in a class
(constructor overloading) with different parameters.
Example
// Create a Car class
class Car
{
public string model; // Create a field

// Create a class constructor for the Car class


public Car()
{
model = "Mustang"; // Set the initial value for model
}

static void Main(string[] args)


{
Car Ford = new Car(); // Create an object of the Car Class (this will call
the constructor)
Console.WriteLine(Ford.model); // Print the value of model
}
}

// Outputs "Mustang"
Constructor Example

Parameters class Car


{
public string model;

 Constructors can also take


parameters, which is used to // Create a class constructor with a
parameter
initialize fields.
public Car(string modelName)
 The example adds a string {
modelName parameter to the model = modelName;
constructor. Inside the }
constructor we set model to
modelName
static void Main(string[] args)
(model=modelName). When we
{
call the constructor, we pass a
Car Ford = new Car("Mustang");
parameter to the constructor
("Mustang"), which will set Console.WriteLine(Ford.model);
the value of model to }
"Mustang" }

// Outputs "Mustang"
Access Modifiers
They are used to set the access level/visibility for classes, fields,
methods and properties.

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.
Private Modifier
 If you declare a field with a // Example: This would give an error
private access modifier, it can class Car
only be accessed within the same {
class:
private string model = "Mustang";
}
 Example

class Program
class Car
{
{
static void Main(string[] args)
private string model = "Mustang";
{
Car myObj = new Car();
static void Main(string[] args)
Console.WriteLine(myObj.model);
{
}
Car myObj = new Car();
}
Console.WriteLine(myObj.model);
}
}
 By default, all members of a class are private if you don't specify an access
modifier
 Example
class Car
{
string model; // private
string year; // private
}
Public Modifier
 If you declare a field with a public access modifier, it is accessible for all classes:
 Example
class Car
{
public string model = "Mustang";
}

class Program
{
static void Main(string[] args)
{
Car myObj = new Car();
Console.WriteLine(myObj.model);
}
}

// The output will be: Mustang


Why Access Modifiers?

 To control the visibility of class members (the security level of each individual
class and class member).

 To achieve "Encapsulation" - which is the process of making sure that


"sensitive" data is hidden from users. This is done by declaring fields as
private.
Encapsulation
 Encapsulation Hides the internal
state and functionality of an object
and only allows access through a
public set of functions.
 The process of binding or grouping
the State (i.e., Data Members) and
Behaviour (i.e., Member Functions)
together into a single unit (i.e.,
class, interface, struct, etc.) is
called Encapsulation in C#.
 The Encapsulation Principle
ensures that the state and
behavior of a unit (i.e., class,
interface, struct, etc.) cannot be
accessed directly from other units
(i.e., class, interface, struct, etc.).
Implementing Data Hiding or Data
Encapsulation in C#

 By declaring the variables as private (to restrict their direct access from
outside the class)
 By defining one pair of public setter and getter methods or properties to
access private variables from outside the class.
//Public Setter Method

Example //This method is used to store the data in the balance variable

public void SetBalance(double balance)


using System;
{
namespace EncapsulationDemo
// add validation logic to check whether data is correct or not
{
this.balance = balance;
public class Bank
}
{
}
//Hiding class data by declaring the
variable as private class Program
private double balance; {

public static void Main()


//Creating public Setter and Getter {
methods
Bank bank = new Bank();

//You cannot access the Private Variable


//Public Getter Method
//bank.balance; //Compile Time Error
//This method is used to return the data
stored in the balance variable

public double GetBalance() //You can access the private variable via public setter and getter methods

{ bank.SetBalance(500);

//add validation logic if needed Console.WriteLine(bank.GetBalance());

return balance; Console.ReadKey();

} }

You might also like