understanding-object-orientation-in-c-slides
understanding-object-orientation-in-c-slides
Programming in C#
Understanding Object-orientation in C#
Gill Cleeren
CTO Xpirit Belgium
@gillcleeren
Overview
Understanding OOP in C#
Working with classes and objects
The pillars of OO
A word of warning!
This will be a more theoretical module!
Take a look at
“C# Fundamentals” if needed
Version Check
This version was created by using:
- C# 12
- Visual Studio 2022
Version Check
This course is 100% applicable to:
- C# 10, 11 & 12
- Visual Studio 2022 (any edition)
Object-oriented programming
Over 50 years old
Moved from C++ and then into Java
C# is Microsoft’s answer
Understanding OOP
Ruby JavaScript… C#
C# and OOP
Object-oriented
Type-safe
Reusable code
Frequent updates
Modern tools
Properties Interfaces
Working with Classes and Objects
Understanding Classes
Blueprint of an object
Fields Methods
Properties Events
Exploring a Simple Class
public class Employee
{
public Employee()
{
//Initialization code goes here
}
Access modifiers
Constructors Are Used for Initialization
public class Employee
{
public Employee()
{
//Initialization code goes here
}
Object 1
FirstName: John
Age: 27
Object 2
FirstName: Mary
Age: 31
Object 3
Employee Class FirstName: George
Age: 22
Age is a field
Employee employee = new Employee();
Instantiating an Object
The new operator
employee.PerformWork();
employee.PerformWork(10);
employee.FirstName = "Gill";
Abstraction Encapsulation
Inheritance Polymorphism
The Pillars of OOP
Abstraction Encapsulation
Inheritance Polymorphism
The concept of abstraction
- Think about the essential concepts, not the
background details
- Create layer of abstraction
- Expose simple handles to interact without
knowing about the details
- Focus on what it does, not how it does it
Abstraction applied on Car
- Steering wheel, brake pedal expose simple
interface to the user
• This is the abstraction
- No need to know how the engine, brakes
really work
• Only necessary aspects are exposed
Benefits of Applying Abstraction
Name
Age
Weight
Address
Hair color
Perform work
Employee
The Pillars of OOP
Abstraction Encapsulation
Inheritance Polymorphism
The Pillars of OOP
Abstraction Encapsulation
Inheritance Polymorphism
Encapsulation
- All data and functionality on this data is
encapsulated inside object
- Only certain information is exposed
- Data is hidden inside object
- Use access modifiers
Encapsulation applied on a Car
- Defines speed, engine, drive(), speedUp()…
- Bound together in one unit
- Engine data must be hidden (private)
- speedUp() can be working on data but still
be publicly available
public class Car t Public part is useable from outside
{
t Private part is only useable from within the
private int id; class itself
private int temperature; t Important data can be hidden so it can’t be
changed (intentionally or by mistake)
public int Id
{
get { return id; }
t We can control how the data changes
set {
if(id > 0)
id = value;
}
}
}
The Pillars of OOP
Abstraction Encapsulation
Inheritance Polymorphism
The Pillars of OOP
Abstraction Encapsulation
Inheritance Polymorphism
Inheritance
- Inherit features from other classes
- Create hierarchy
• Is-A relation
- Improve reuse of code between parent and
child class
- Child class can extend functionality and
attributes
Inheritance Applied on a Car
public class Car
{
private int maxSpeed;
public int MaxSpeed { get => maxSpeed; set => maxSpeed = value; }
Abstraction Encapsulation
Inheritance Polymorphism
The Pillars of OOP
Abstraction Encapsulation
Inheritance Polymorphism
Polymorphism
- Allow methods to execute differently
- virtual and override keywords in C#
- On objects of different types, we can invoke
the same methods
Applying Method Overriding