GCSE Link: None
Object-Oriented Programming (OOP) is a programming paradigm in which everything in a program is an object.
Each object is a specific instance of a class, which is a template for an object (containing methods and attributes that all objects of that class need).
Example 1 shows an example of a class in C#.
Example 1
class Person(string name) {
public string Name = name;
public void Greet() {
Console.WriteLine("Hi,
I'm " + Name);
}
}
This class contains an attribute Name, and a method Greet().
Instantiation is the process of creating an object (an instance of a class).
We can create an instance of the Person class using the
new keyword and passing in the name.
Example 2 shows an example of instantiation in C#.
Example 2
Person a = new("Adam");
a.Greet();
Person b = new("Bob");
b.Greet();
Although they are part of the same class, a and b are different objects,
so they have different values of their attributes, and therefore the behaviour of their methods
is different.
Encapsulation is the OOP principle that allows data and methods to be bundled into objects.
State the output of Example 2.
Hi, I'm Adam
Hi, I'm Bob