SlideShare a Scribd company logo
2
Most read
3
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

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
 

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
 
Ad

Recently uploaded (20)

PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 

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