SlideShare a Scribd company logo
2
Most read
3
Most read
12
Most read
C#
LECTURE#9
Abid Kohistani
GC Madyan Swat
What is OOP?
OOP stands for Object-Oriented Programming.
Procedural programming is about writing procedures or methods that perform operations on the data, while
object-oriented programming is about creating objects that contain both data and methods.
Object-oriented programming has several advantages over procedural programming:
OOP is faster and easier to execute
OOP provides a clear structure for the programs
OOP helps to keep the C# code DRY "Don't Repeat Yourself", and makes the code easier to maintain, modify and
debug
OOP makes it possible to create full reusable applications with less code and shorter development time
Tip: The "Don't Repeat Yourself" (DRY) principle is about reducing the repetition of code. You should
extract out the codes that are common for the application, and place them at a single place and reuse
them instead of repeating it.
What are Classes and Objects?
Classes and objects are the two main aspects of object-oriented programming.
Look at the following illustration to see the difference between class and objects:
Class
Fruit
Apple
objects
Vehicle Class
Audie object
What are Classes and Objects?
◦ So, a class is a template for objects, and an object is an instance of a class.
◦ When the individual objects are created, they inherit all the variables and
methods from the class.
◦ You will learn much more about classes and objects in the next chapter.
Classes and Objects
◦ Everything in C# is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object.
The car has attributes, such as weight and color, and methods, such as drive and brake.
◦ A Class is a "blueprint" for creating objects.
Create a Class: To create a class, use the class keyword:
Create a class named "Car" with a variable color.
class Car
{
string color = "red";
}
Example
When a variable is declared directly in a class, it is
often referred to as a field (or attribute).
It is not required, but it is a good practice to start
with an uppercase first letter when naming classes.
Also, it is common that the name of the C# file and
the class matches, as it makes our code organized.
However it is not required (like in Java).
Create an Object
An object is created from a class. We have already created the class named Car, so now we can use this to create objects.
To create an object of Car, specify the class name, followed by the object name, and use the keyword new:
Example:
Create an object called "myObj" and use it to print the value of color:Example
class Car
{
string color = "red";
static void Main(string[] args)
{
Car myObj = new Car();
Console.WriteLine(myObj.color);
}
}
Note that we use the dot syntax (.) to access
variables/fields inside a class (myObj.color). You will
learn more about fields in the next chapter.
You can create multiple objects
of one class:
class Car
{
string color = "red";
static void Main(string[] args)
{
Car myObj1 = new Car();
Car myObj2 = new Car();
Console.WriteLine(myObj1.color);
Console.WriteLine(myObj2.color);
}
}
Using Multiple Classes
You can also create an object of a class and access it in another class. This is often used for better organization of classes (one class has all the
fields and methods, while the other class holds the Main() method (code to be executed)).
Example
Did you notice the public keyword? It is
called an access modifier, which specifies
that the color variable/field of Car is
accessible for other classes as well, such as
Program.
Class calling car class object
class Car
{
public string color = "red";
}
class Program
{
static void Main(string[] args)
{
Car myObj = new Car();
Console.WriteLine(myObj.color);
}
}
Class Members
Fields and methods inside classes are often referred to as "Class Members":
Create a Car class with three class members: two fields and one method.
Example
// The class
class MyClass
{
// Class members
string color = "red";// field
int maxSpeed = 200;// field
public void fullThrottle() // method
{
Console.WriteLine("The car is going as fast as it can!");
}
}
In the previous chapter, you learned that variables inside a class are called fields, and
that you can access them by creating an object of the class, and by using the
dot syntax (.).
The following example will create an object of the Car class, with the name myObj.
Then we print the value of the fields color and maxSpeed:
Fields
class Car
{
string color = "red";
int maxSpeed = 200;
static void Main(string[] args)
{
Car myObj = new Car();
Console.WriteLine(myObj.color);
Console.WriteLine(myObj.maxSpeed);
}
}
Example with blank fields
class Car
{
string model;
string color;
int year;
static void Main(string[] args)
{
Car Ford = new Car();
Ford.model = "Mustang";
Ford.color = "red";
Ford.year = 1969;
Car Opel = new Car();
Opel.model = "Astra";
Opel.color = "white";
Opel.year = 2005;
Console.WriteLine(Ford.model);
Console.WriteLine(Opel.model);
}
}
Object Methods
Methods normally belongs to a class, and they define how an object of a class behaves.
Just like with fields, you can access methods with the dot syntax. However, note that the method must be public. And remember that we use
the name of the method followed by two parentheses ( ) and a semicolon ; to call (execute) the method:
Why did we declare the method as public, and not
static, like in the examples from the C# Methods
Chapter?
The reason is simple: a static method can be accessed
without creating an object of the class, while public
methods can only be accessed by objects.
class Car
{
string color; // field
int maxSpeed; // field
public void fullThrottle() // method
{
Console.WriteLine("The car is going as fast as it can!");
}
static void Main(string[] args)
{
Car myObj = new Car();
myObj.fullThrottle(); // Call the method
}
}
Use Multiple Classes
Remember from the last chapter, that we can use multiple classes for better organization (one for fields and methods, and another one for
execution). This is recommended:
The public keyword is called an access modifier, which specifies that the fields of
Car are accessible for other classes as well, such as Program..
class Car
{
public string model;
public string color;
public int year;
public void fullThrottle()
{
Console.WriteLine("The car is going as fast as it can!");
}
}
class Program
{
static void Main(string[] args)
{
Car Ford = new Car();
Ford.model = "Mustang";
Ford.color = "red";
Ford.year = 1969;
Car Opel = new Car();
Opel.model = "Astra";
Opel.color = "white";
Opel.year = 2005;
Console.WriteLine(Ford.model);
Console.WriteLine(Opel.model);
}
}
Example
END

More Related Content

PPTX
Access Modifiers in C# ,Inheritance and Encapsulation
Abid Kohistani
 
PPTX
Exception Handling in C#
Abid Kohistani
 
PDF
Xamarin: Inheritance and Polymorphism
Eng Teong Cheah
 
PPT
Abstract class
Hoang Nguyen
 
PPTX
Polymorphism in java
Elizabeth alexander
 
PDF
Java OOP Programming language (Part 6) - Abstract Class & Interface
OUM SAOKOSAL
 
PPTX
Abstract Class & Abstract Method in Core Java
MOHIT AGARWAL
 
PPT
Chapter 9 Interface
OUM SAOKOSAL
 
Access Modifiers in C# ,Inheritance and Encapsulation
Abid Kohistani
 
Exception Handling in C#
Abid Kohistani
 
Xamarin: Inheritance and Polymorphism
Eng Teong Cheah
 
Abstract class
Hoang Nguyen
 
Polymorphism in java
Elizabeth alexander
 
Java OOP Programming language (Part 6) - Abstract Class & Interface
OUM SAOKOSAL
 
Abstract Class & Abstract Method in Core Java
MOHIT AGARWAL
 
Chapter 9 Interface
OUM SAOKOSAL
 

What's hot (20)

PPT
06 abstract-classes
Anup Burange
 
PPT
Java Programming - Abstract Class and Interface
Oum Saokosal
 
PPT
Java interfaces & abstract classes
Shreyans Pathak
 
PDF
What are Abstract Classes in Java | Edureka
Edureka!
 
PPTX
Abstract method
Yaswanth Babu Gummadivelli
 
PPT
Abstract class in java
Lovely Professional University
 
PDF
javainterface
Arjun Shanka
 
PPTX
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
Simplilearn
 
PPTX
Java interfaces
jehan1987
 
PPT
Poo java
Harry Potter
 
PPT
Lecture13 abap on line
Milind Patil
 
PPTX
Interfaces and abstract classes
AKANSH SINGHAL
 
PPT
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
cprogrammings
 
PPTX
Friend functions
Megha Singh
 
PDF
8 abstract classes and interfaces
Tuan Ngo
 
PPTX
Inheritance, friend function, virtual function, polymorphism
Jawad Khan
 
PPTX
Abstract & Interface
Linh Lê
 
PPTX
Java interfaces
Elizabeth alexander
 
PPTX
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
Simplilearn
 
PPT
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
06 abstract-classes
Anup Burange
 
Java Programming - Abstract Class and Interface
Oum Saokosal
 
Java interfaces & abstract classes
Shreyans Pathak
 
What are Abstract Classes in Java | Edureka
Edureka!
 
Abstract class in java
Lovely Professional University
 
javainterface
Arjun Shanka
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
Simplilearn
 
Java interfaces
jehan1987
 
Poo java
Harry Potter
 
Lecture13 abap on line
Milind Patil
 
Interfaces and abstract classes
AKANSH SINGHAL
 
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
cprogrammings
 
Friend functions
Megha Singh
 
8 abstract classes and interfaces
Tuan Ngo
 
Inheritance, friend function, virtual function, polymorphism
Jawad Khan
 
Abstract & Interface
Linh Lê
 
Java interfaces
Elizabeth alexander
 
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
Simplilearn
 
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
Ad

Similar to OOP in C# Classes and Objects. (20)

PPTX
classes object fgfhdfgfdgfgfgfgfdoop.pptx
arjun431527
 
PPTX
C# classes objects
Dr.Neeraj Kumar Pandey
 
PPTX
Object-Oriented Programming with C#
Svetlin Nakov
 
PPT
Constructor
abhay singh
 
PPTX
CSharp presentation and software developement
frwebhelp
 
PDF
Object-oriented programming (OOP) with Complete understanding modules
Durgesh Singh
 
PPTX
03 classes interfaces_principlesofoop
Vladislav Hadzhiyski
 
PDF
Object Oriented Programming with C#
SyedUmairAli9
 
PPT
Object Oriented Programming (Advanced )
ayesha420248
 
DOC
C# by Zaheer Abbas Aghani
Information Technology Center
 
DOC
C# by Zaheer Abbas Aghani
Information Technology Center
 
PPTX
Notes(1).pptx
InfinityWorld3
 
PPTX
Better Understanding OOP using C#
Chandan Gupta Bhagat
 
DOCX
Introduction to object oriented programming concepts
Ganesh Karthik
 
PPTX
5. c sharp language overview part ii
Svetlin Nakov
 
PPTX
Classes and Objects in C#
Adeel Rasheed
 
PPTX
Module 6 : Essentials of Object Oriented Programming
Prem Kumar Badri
 
PPTX
C# overview part 2
sagaroceanic11
 
PPTX
C# - Igor Ralić
Software StartUp Academy Osijek
 
classes object fgfhdfgfdgfgfgfgfdoop.pptx
arjun431527
 
C# classes objects
Dr.Neeraj Kumar Pandey
 
Object-Oriented Programming with C#
Svetlin Nakov
 
Constructor
abhay singh
 
CSharp presentation and software developement
frwebhelp
 
Object-oriented programming (OOP) with Complete understanding modules
Durgesh Singh
 
03 classes interfaces_principlesofoop
Vladislav Hadzhiyski
 
Object Oriented Programming with C#
SyedUmairAli9
 
Object Oriented Programming (Advanced )
ayesha420248
 
C# by Zaheer Abbas Aghani
Information Technology Center
 
C# by Zaheer Abbas Aghani
Information Technology Center
 
Notes(1).pptx
InfinityWorld3
 
Better Understanding OOP using C#
Chandan Gupta Bhagat
 
Introduction to object oriented programming concepts
Ganesh Karthik
 
5. c sharp language overview part ii
Svetlin Nakov
 
Classes and Objects in C#
Adeel Rasheed
 
Module 6 : Essentials of Object Oriented Programming
Prem Kumar Badri
 
C# overview part 2
sagaroceanic11
 
Ad

More from Abid Kohistani (7)

PPT
Algorithm in Computer, Sorting and Notations
Abid Kohistani
 
PPTX
Polymorphism in C# Function overloading in C#
Abid Kohistani
 
PPTX
Loops in C# for loops while and do while loop.
Abid Kohistani
 
PPTX
Conditions In C# C-Sharp
Abid Kohistani
 
PPTX
C# Operators. (C-Sharp Operators)
Abid Kohistani
 
PPTX
data types in C-Sharp (C#)
Abid Kohistani
 
PPTX
Methods In C-Sharp (C#)
Abid Kohistani
 
Algorithm in Computer, Sorting and Notations
Abid Kohistani
 
Polymorphism in C# Function overloading in C#
Abid Kohistani
 
Loops in C# for loops while and do while loop.
Abid Kohistani
 
Conditions In C# C-Sharp
Abid Kohistani
 
C# Operators. (C-Sharp Operators)
Abid Kohistani
 
data types in C-Sharp (C#)
Abid Kohistani
 
Methods In C-Sharp (C#)
Abid Kohistani
 

Recently uploaded (20)

PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
This slide provides an overview Technology
mineshkharadi333
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
PDF
GYTPOL If You Give a Hacker a Host
linda296484
 
DOCX
Top AI API Alternatives to OpenAI: A Side-by-Side Breakdown
vilush
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Why Your AI & Cybersecurity Hiring Still Misses the Mark in 2025
Virtual Employee Pvt. Ltd.
 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
PPTX
C Programming Basics concept krnppt.pptx
Karan Prajapat
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PDF
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PDF
DevOps & Developer Experience Summer BBQ
AUGNYC
 
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
georgschmitzdoerner
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Doc9.....................................
SofiaCollazos
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
This slide provides an overview Technology
mineshkharadi333
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
GYTPOL If You Give a Hacker a Host
linda296484
 
Top AI API Alternatives to OpenAI: A Side-by-Side Breakdown
vilush
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Why Your AI & Cybersecurity Hiring Still Misses the Mark in 2025
Virtual Employee Pvt. Ltd.
 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
C Programming Basics concept krnppt.pptx
Karan Prajapat
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
DevOps & Developer Experience Summer BBQ
AUGNYC
 
madgavkar20181017ppt McKinsey Presentation.pdf
georgschmitzdoerner
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 

OOP in C# Classes and Objects.

  • 2. What is OOP? OOP stands for Object-Oriented Programming. Procedural programming is about writing procedures or methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods. Object-oriented programming has several advantages over procedural programming: OOP is faster and easier to execute OOP provides a clear structure for the programs OOP helps to keep the C# code DRY "Don't Repeat Yourself", and makes the code easier to maintain, modify and debug OOP makes it possible to create full reusable applications with less code and shorter development time Tip: The "Don't Repeat Yourself" (DRY) principle is about reducing the repetition of code. You should extract out the codes that are common for the application, and place them at a single place and reuse them instead of repeating it.
  • 3. What are Classes and Objects? Classes and objects are the two main aspects of object-oriented programming. Look at the following illustration to see the difference between class and objects: Class Fruit Apple objects Vehicle Class Audie object
  • 4. What are Classes and Objects? ◦ So, a class is a template for objects, and an object is an instance of a class. ◦ When the individual objects are created, they inherit all the variables and methods from the class. ◦ You will learn much more about classes and objects in the next chapter.
  • 5. Classes and Objects ◦ Everything in C# is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake. ◦ A Class is a "blueprint" for creating objects. Create a Class: To create a class, use the class keyword: Create a class named "Car" with a variable color. class Car { string color = "red"; } Example When a variable is declared directly in a class, it is often referred to as a field (or attribute). It is not required, but it is a good practice to start with an uppercase first letter when naming classes. Also, it is common that the name of the C# file and the class matches, as it makes our code organized. However it is not required (like in Java).
  • 6. Create an Object An object is created from a class. We have already created the class named Car, so now we can use this to create objects. To create an object of Car, specify the class name, followed by the object name, and use the keyword new: Example: Create an object called "myObj" and use it to print the value of color:Example class Car { string color = "red"; static void Main(string[] args) { Car myObj = new Car(); Console.WriteLine(myObj.color); } } Note that we use the dot syntax (.) to access variables/fields inside a class (myObj.color). You will learn more about fields in the next chapter. You can create multiple objects of one class: class Car { string color = "red"; static void Main(string[] args) { Car myObj1 = new Car(); Car myObj2 = new Car(); Console.WriteLine(myObj1.color); Console.WriteLine(myObj2.color); } }
  • 7. Using Multiple Classes You can also create an object of a class and access it in another class. This is often used for better organization of classes (one class has all the fields and methods, while the other class holds the Main() method (code to be executed)). Example Did you notice the public keyword? It is called an access modifier, which specifies that the color variable/field of Car is accessible for other classes as well, such as Program. Class calling car class object class Car { public string color = "red"; } class Program { static void Main(string[] args) { Car myObj = new Car(); Console.WriteLine(myObj.color); } }
  • 8. Class Members Fields and methods inside classes are often referred to as "Class Members": Create a Car class with three class members: two fields and one method. Example // The class class MyClass { // Class members string color = "red";// field int maxSpeed = 200;// field public void fullThrottle() // method { Console.WriteLine("The car is going as fast as it can!"); } } In the previous chapter, you learned that variables inside a class are called fields, and that you can access them by creating an object of the class, and by using the dot syntax (.). The following example will create an object of the Car class, with the name myObj. Then we print the value of the fields color and maxSpeed: Fields class Car { string color = "red"; int maxSpeed = 200; static void Main(string[] args) { Car myObj = new Car(); Console.WriteLine(myObj.color); Console.WriteLine(myObj.maxSpeed); } }
  • 9. Example with blank fields class Car { string model; string color; int year; static void Main(string[] args) { Car Ford = new Car(); Ford.model = "Mustang"; Ford.color = "red"; Ford.year = 1969; Car Opel = new Car(); Opel.model = "Astra"; Opel.color = "white"; Opel.year = 2005; Console.WriteLine(Ford.model); Console.WriteLine(Opel.model); } }
  • 10. Object Methods Methods normally belongs to a class, and they define how an object of a class behaves. Just like with fields, you can access methods with the dot syntax. However, note that the method must be public. And remember that we use the name of the method followed by two parentheses ( ) and a semicolon ; to call (execute) the method: Why did we declare the method as public, and not static, like in the examples from the C# Methods Chapter? The reason is simple: a static method can be accessed without creating an object of the class, while public methods can only be accessed by objects. class Car { string color; // field int maxSpeed; // field public void fullThrottle() // method { Console.WriteLine("The car is going as fast as it can!"); } static void Main(string[] args) { Car myObj = new Car(); myObj.fullThrottle(); // Call the method } }
  • 11. Use Multiple Classes Remember from the last chapter, that we can use multiple classes for better organization (one for fields and methods, and another one for execution). This is recommended: The public keyword is called an access modifier, which specifies that the fields of Car are accessible for other classes as well, such as Program.. class Car { public string model; public string color; public int year; public void fullThrottle() { Console.WriteLine("The car is going as fast as it can!"); } } class Program { static void Main(string[] args) { Car Ford = new Car(); Ford.model = "Mustang"; Ford.color = "red"; Ford.year = 1969; Car Opel = new Car(); Opel.model = "Astra"; Opel.color = "white"; Opel.year = 2005; Console.WriteLine(Ford.model); Console.WriteLine(Opel.model); } } Example
  • 12. END