C# – What is OOP?
OOP means Object-Oriented Programming.
Writing methods or procedures that operate on data is what procedural programming entails, whereas object-oriented programming focuses on building objects that hold both data and methods.
Compared to procedural programming, object-oriented programming offers the following benefits:
- OOP is simpler and faster to implement.
- OOP gives the applications a distinct framework.
- OOP facilitates the “Don’t Repeat Yourself” approach to C# code, making it simpler to maintain, alter, and debug.
- With OOP, whole reusable applications may be developed more quickly and with less code.
C# – What are Classes and Objects?
The two primary components of object-oriented programming are classes and objects.
Examine the following example to understand the distinction between objects and classes:
class
Flowers
objects
Rose
Lilly
Mariglod
One more example:
class
Classroom
objects
Black board
Notebook
Chalk piece
In other words, an object is an instance of a class, and a class is a template for objects.
All of the class’s variables and methods are passed down to the individual objects upon creation.
The next chapter will cover a lot more information on classes and objects.
Classes and Objects
The last chapter introduced you to the object-oriented programming language, C#.
In C#, everything is connected to classes and objects, as well as their methods and attributes. For instance, an automobile is an object in the actual world. The car contains features like color and weight, as well as functions like brake and drive.
A class is a “blueprint” for building objects, similar to an object constructor.
Create a Class
Use the class keyword to create a class:
Create a class named “Car” with a variable color:
class Car
{
string color=”red”;
}
A variable is frequently referred to as a field (or attribute) when it is specified directly in a class.
While it’s not necessary, it’s a good idea to begin class names with an uppercase letter. Additionally, it is typical for the class name and the C# file to match, as this helps to organize our code. It is not necessary, though (as in Java).
Create an Object
Example
Multiple Objects
Example
Using Multiple Classes
- prog2.cs
- prog.cs
Class Members
Example
Fields
Example
Example
Example
Object Methods
Example
Use Multiple Classes
prog2.cs
prog.cs
A later chapter will have more information regarding Access Modifiers.
As you go on, you’ll discover additional information on constructors and properties, among other class members.
Constructors
Example
Constructor Parameters
Example
Example
Constructors Save Time
Without Constructor:
prog.cs
With Constructor:
prog.cs
Access Modifiers
You should be somewhat familiar with the public keyword by now, as it shows up in a lot of our examples:
public string color;
Access modifiers, such as the public keyword, are used to control the visibility and level of access to classes, fields, methods, and properties.
These are the access modifiers available in C#:
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. You will learn more about inheritance in a later chapter |
internal | The code is only accessible within its own assembly, but not from another assembly. You will learn more about this in a later chapter |
Additionally, there are two combinations: private protected and protected internal.
Let us now concentrate on both public and private modifiers.
Private Modifier
Example
Example
The field ‘Car.model’ is assigned but its value is never used
Public Modifier
Example
Why Access Modifiers?
To ensure that “sensitive” data is concealed from users, a process known as “encapsulation” must be completed. Fields are made private by declaring them as so. We’ll talk more about this in the upcoming chapter.
Note: If you omit to include an access modifier, all members of a class are private by default:
Example
- Declare variables and fields as private.
- Then, using properties, give the public get and set methods to access and update the value of a private field.
Properties
Example
Example explained
Example
Automatic Properties (Short Hand)
Example
Using automatic properties:
Why Encapsulation?
- Improved member control in the classroom (less chance that you or anybody else will make a coding error)
- Write-only and read-only options are available for fields (assuming you use the set method exclusively).
- Programmers can modify a single section of the code without affecting other sections, making it flexible.
- Enhanced data security
Inheritance (Derived and Base Class)
- Derived class (child):The class that derives from another class.
- Base Class(parent): This is the class that one is inheriting from.
Example
Reusing fields and methods from an existing class when creating a new one is helpful for code reusability.
A hint: Also, check out Polymorphism, the following chapter, which employs inherited techniques to carry out various operations.
The sealed Keyword
When attempting to access a sealed class, C# will produce the following error:
sealed class Vehicle
{
…
}
class Car : Vehicle
{
….
}
The error message will be something like this:
‘Car’ : cannot derive from sealed type ‘Vehicle’