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.java

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;
        System.out.println(stu1.name());
    }
}

Student Class

public class Student {
    String name;
    int 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.java

public class Main {
    public static void main(String[] args) {
        Student stu1 = new Student();
        stu1.setName("Ahmed");
        System.out.println(stu1.getName());
        // Method is used to access the attribute.
    }
}

Student Class

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.
}

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.java

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

Student Class

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.
}

Cmd Output:

Ahmed
15
Victor
16

By using concatenation you can display the data better.

Main.java

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

Student Class

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.
}

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.java

public class Main {
    public static void main(String[] args) {
        Student stu1 = new Student();
        stu1.setName("Ahmed");
        stu1.setAge(15);
        System.out.println(stu1.getName()+" is "+stu1.getAge());
        Student stu2 = new Student();
        stu2.setName("Victor");
        stu2.setAge(16);
        System.out.println(stu2.getName()+" is "+stu2.getAge());
    }
}
// Errors occur when the constructor is created because parameters are expected when the object is being created.

Student Class

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;
    }
}
// Constructor created like a function in the super class.

Cmd Output:

Ahmed

Main.java

public class Main {
    public static void main(String[] args) {
        Student stu1 = new Student("Ahmed",15);
        System.out.println(stu1.getName()+" is "+stu1.getAge());
        Student stu2 = new Student("Victor",16);
        System.out.println(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);

Student Class

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;
    }
}
// Constructor created like a function in the super class.

Cmd Output:

Ahmed

3