1

mrahmedcomputing

KS3, GCSE, A-Level Computing Resources

Lesson 7. Object Oriented Programming


Lesson Objective

  • Understand the basics of Object Oriented Programming.
  • Define a Class.
  • Set attribute and create methods.
  • Understand the basics of Encapsulation.
  • Instantiate an Object.
  • Use Constructors to create objects and set values.

Lesson Notes

Procedural Programming

Procedural programming is a style of programming that divides a program into a set of subroutines.

Data is stored in variables and functions operate on that data.

This style of programming is simple and straightforward, but as programs grow, it can become difficult to manage. Procedural code can become difficult to understand and maintain.

Object-oriented programming (OOP) came to solve this problem.


Object Oriented Programming

Groups of related variables and subroutines are grouped together into classes.

A class specifies all the attributes and methods.

Classes become blueprints for objects and define how objects will look and what they do.

main.c#

public class Student
{
    public string Name;
    public int Age;
}

public class Main
{
    public static void Main(string[] args)
    {
        Student stu1 = new Student();
        stu1.Name = "Ahmed";
        stu1.Age = 15;

        Student stu2 = new Student();
        stu2.Name = "Victor";
        stu2.Age = 16;

        Console.WriteLine("Student 1:");
        Console.WriteLine("Name: " + stu1.Name);
        Console.WriteLine("Age: " + stu1.Age);

    }
}

Cmd Output:

Ahmed

Encapsulation

All attributes and methods are wrapped in the class.

By doing this attributes can only be changed through the associated class methods.

Attributes should only be visible in the scope of the class. This is referred to as “data hiding” as implementation details are hidden from the user. This is a key aspect of OOP.

main.c#

public class Student {
    private string name;
    private int age;
    // Attributes set to private so they can only be accessed in the class.
    public string getName() {
        return name;
    }
    public void setName(string n) {
        name = n;
    }
    // Methods have been created to access the name attribute.
}
public class Main {
    public static void main(string[] args) {
        Student stu1 = new Student();
        stu1.setName("Ahmed");
        Console.WriteLine(stu1.getName());
        // Method is used to access the attribute.
    }
}

Cmd Output:

Ahmed

Getters and Setters

Encapsulation refers to the process of hiding "sensitive" information from users. In order to accomplish this, certain steps need to be taken:

Private variables are restricted to access within the same class, meaning an external class has no access to it. However, it is possible to access them if we provide public getters and setters.

The get method returns the variable value, and the set method sets the value of the variable.

The syntax for both methods follows a specific pattern: they begin with either "get" or "set", followed by the variable name with the first letter capitalized.

main.c#

public class Student {
    private String name;
    private int age;
    
    public String getName() {
        return name;
    }
    public void setName(String n) {
        name = n;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int a) {
        age = a;
    }
    // Methods have now been created to access the age attribute.
}

public class Main {
    public static void main(String[] args) {
        Student stu1 = new Student();
        stu1.setName("Ahmed");
        stu1.setAge(15);
        Console.WriteLine(stu1.getName());
        Console.WriteLine(stu1.getAge());
        Student stu2 = new Student();
        stu2.setName("Victor");
        stu2.setAge(16);
        Console.WriteLine(stu2.getName());
        Console.WriteLine(stu2.getAge());
    }
}

Cmd Output:

Ahmed
15
Victor
16

By using concatenation you can display the data better.

main.c#

public class Student {
    private string name;
    private int age;
    
    public string getName() {
        return name;
    }
    public void setName(string n) {
        name = n;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int a) {
        age = a;
    }
    // Methods have now been created to access the age attribute.
}

public class Main {
    public static void main(string[] args) {
        Student stu1 = new Student();
        stu1.setName("Ahmed");
        stu1.setAge(15);
        Console.WriteLine(stu1.getName()+" is "+stu1.getAge());
        Student stu2 = new Student();
        stu2.setName("Victor");
        stu2.setAge(16);
        Console.WriteLine(stu2.getName()+" is "+stu2.getAge());
    }
}
// Concatenation to display the data better.

Cmd Output:

Ahmed is 15
Victor is 17

Constructor

Instead of creating the object and then setting the values. You can create a constructor that creates the object and sets the values at the same time.

main.c#

public class Student {
    private String name;
    private int age;
    public Student (String n, int a){
        name = n;
        age = a;
    }
    public String getName() {
        return name;
    }
    public void setName(String n) {
        name = n;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int a) {
        age = a;
    }
}

// Errors occur when the constructor is created because parameters are expected when the object is being created.

public class Main {
    public static void main(String[] args) {
        Student stu1 = new Student();
        stu1.setName("Ahmed");
        stu1.setAge(15);
        Console.WriteLine(stu1.getName()+" is "+stu1.getAge());
        Student stu2 = new Student();
        stu2.setName("Victor");
        stu2.setAge(16);
        Console.WriteLine(stu2.getName()+" is "+stu2.getAge());
    }
}

Cmd Output:

Ahmed

main.c#

public class Student {
    private String name;
    private int age;
    public Student (String n, int a){
        name = n;
        age = a;
    }
    public String getName() {
        return name;
    }
    public void setName(String n) {
        name = n;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int a) {
        age = a;
    }
}

public class Main {
    public static void main(String[] args) {
        Student stu1 = new Student("Ahmed",15);
        Console.WriteLine(stu1.getName()+" is "+stu1.getAge());
        Student stu2 = new Student("Victor",16);
        Console.WriteLine(stu2.getName()+" is "+stu2.getAge());
    }
}
// Constructor is applied to instantiate and set values.
// Note:
// This concept has been used during the use of the Scanner Class.
// Scanner scan_ob = new Scanner (System.in);

Cmd Output:

Ahmed