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.
  • Understand Inheritance.
  • Understand Polymorphism.

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.

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.

Code:

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.

Code:

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.

Code:

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.

Code:

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.

Code:

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

Code:

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

Inheritance

In Java, it is possible to inherit attributes and methods from one class to another. The "inheritance concept" is categorized into two groups:

In Java, to inherit from a class, use the extends keyword.

In the following example, the P16_Student class (subclass) inherits the attributes and methods from the Student class (superclass)

Code:

Main.java
public class Main {
    public static void main(String[] args) {
       P16_Student stu24 = new P16_Student();
       stu24.setName("Fei");
       stu24.setAge(17);
       System.out.print(stu24.getName());
       System.out.print(" is ");
       System.out.println(stu24.getAge());
    }
}
// New class inherits from Student superclass.
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;
    }
}
// Note: Constructor has been removed in this example.
P16_Student Class
public class P16_Student extends Student{

}

Cmd Output:

Fei is 17

super is used to refer to the superclass, which will send data to the constructor in the superclass to the subclass.

Code:

Main.java
public class Main {
  public static void main(String[] args) {
      P16_Student stu24 = new P16_Student("Fei",17,false);
      System.out.println(stu24.getName()+" is "+stu24.getAge());
      System.out.println("UCAS complete: "+stu24.getUCAS());
    }
}
// Method specific to the P16_Student Class used here.
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;
    }
}
// Original Student Constructor has been placed back into the Student Class.
P16_Student Class
public class P16_Student extends Student{
    private boolean UCAS;

  public P16_Student(String n,int a,boolean u){
       super (n,a);
       UCAS = u;
   }
   public boolean getUCAS() {
       return UCAS;
   }
   public void setUCAS(boolean u) {
       UCAS = u;
   }
}
// New P16_Student Constructor has been created in this class. super is used to specify which parameters are inherited from the student superclass.

Cmd Output:

Fei is 17
UCAS complete: false

Polymorphism

Polymorphism, which translates to "many forms," arises when we have multiple classes connected through inheritance.

Inheritance enables the acquisition of attributes and methods from another class.

Polymorphism, in turn, utilizes these inherited methods to carry out alternate but similar tasks. This flexibility permits the execution of a single action in different ways.

Code:

Main.java
public class Main {
    public static void main(String[] args) {
        P16_Student stu24 = new P16_Student("Fei",17,false);
        System.out.print(stu24.getName());
        System.out.print(" is ");
        System.out.println(stu24.getAge());
        System.out.println("UCAS complete: "+stu24.getUCAS());
        stu24.validAge();

        P16_Student stu25 = new P16_Student("Bram",12,false);
        stu25.validAge();

        Student stu1 = new Student ("Bram", 12);
        stu1.validAge();
    }
}
Student Class
public class Student {
    private String name;
    private int age;
    public Student (String n, int a){
        name = n;
        age = a;
    }
    public void validAge(){
        if (age >=11 && age <= 16) {
            System.out.println("Age Valid");
        }else{
            System.out.println("Age Invalid");
        }
    }
    public String getName() {
        return name;
    }
    public void setName(String n) {
        name = n;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int a) {
        age = a;
    }
}
// Original Student Constructor has been placed back into the Student Class.
P16_Student Class
public class P16_Student extends Student{
    private boolean UCAS;

  public P16_Student(String n,int a,boolean u){
       super (n,a);
       UCAS = u;
   }
   //@Override?
   public void validAge(){
    if (super.getAge() >=16 && super.getAge() <= 19){
         System.out.println("Age Valid");
     }else{
         System.out.println("Age Invalid");
     }
   }
   public boolean getUCAS() {
       return UCAS;
   }
   public void setUCAS(boolean u) {
       UCAS = u;
   }
}

Cmd Output:

Fei is 17
UCAS complete: false
Age Valid
Age Invalid
Age Valid
3