VJC Chapter 13 Object Oriented Programming
Uploaded by cheesemuffin · 10 December 2025
Preview
Text from the first pagesVJC/H2Computing/9569 Chapter 13 Object Oriented Programming (OOP) Contents 1 Components of Object Oriented Programming (OOP) 1.1 Class attributes 1.2 Instance attribute vs Class attribute 2 Encapsulation 3 Inheritance 4 Polymorphism Syllabus Learning Outcomes 2.5 Fundamentals of Object-Oriented Programming Understand concepts of encapsulation, inheritance, and polymorphism. 2.5.1 Define and understand classes and objects. 2.5.2 Understand encapsulation and how classes support information hiding and implementation independence. 2.5.3 Understand inheritance and how it promotes software reuse. 2.5.4 Understand polymorphism and how it enables code generalisation. Exclude: method overloading and multiple inheritance 1
VJC/H2Computing/9569 1 Components of Object-Oriented Programming (OOP) Object-oriented programming (OOP) is a form of programming that groups the data items and the subroutines that operate on the data items in one place so that only these subroutines can access the data items. The data of an object are called attributes (or properties ) and the subroutines acting on the attributes are called methods . The 2 main building blocks of object-oriented programming are classes and objects . A class is a blueprint or a template for an object that contains attributes or properties which describe the data, and methods which can access the attributes. When a class has been defined, it can be used to create one or more objects of this class type. Instantiation is the process of creating a new object. An object is an instance of a class. The objects will have the same attributes and methods as the class from which it was created. A class can be represented using a class diagram. Each class is made up of 3 sections: the class name, properties (attributes) and methods. Class Properties Methods The class diagram below shows an example of a Person class. Person -name -mobile_no constructor() +get_name() +get_mobile_no() +set_name() +set_mobile_no() When we first set up an object of a particular class, we use a constructor. A constructor instantiates the object and assigns initial values to the attributes. A Person class has 2 attributes: name and mobile_no . When we first create a Person object, we use a constructor method __init()__ to instantiate the object and assign values to the attributes. 2
VJC/H2Computing/9569 The constructor method will automatically be called to initialise the newly created object. The first input parameter for all instance methods must be self . The self parameter is referred to the current object of the class, i.e. the instance calling the method. To access any instance method or instance attribute in the class, you need to prefix it with self . Note that the default of the parameter mobile_no can be set as an empty string as not everyone may have a mobile number. Let’s now create a new object: John with mobile number 91234567. Line 10 of the code above shows a new object p1 is created and the object is stored at a particular address. To print out useful information of the object that is readable, we can implement the __str__() method. We can also write a display method to display the details of the data in the class. 3
VJC/H2Computing/9569 Note that when you call an object’s method, you do not need to give a value for the self parameter. Python provides it automatically. 1.1 Class attributes Class Attributes are attributes which belong to the class instead of a particular object. It can be accessed through either class or instance. Class attributes can be used to keep a rolling value which is shared among all instances. For example, we would like to keep track of number of Customers by assigning each customer a unique serial number. Modification to a class attribute can only be done with the notation ClassName.AttributeName . Otherwise, a new instance variable will be created. 4
VJC/H2Computing/9569 5
VJC/H2Computing/9569 1.2 Instance attribute vs Class attribute Instance attributes belong to a particular instance. Modifying instance attribute of an instance does not affect other instances. It is not necessary to declare an attribute for an object before using it either. Note that assigning value to an non-existence attribute will create that attribute. Class attributes are shared among all instances. They can be accessed not only through class but also through an instance. In the above example, what if line 7 is changed to x.cls_attr = "my value" ? 2 Encapsulation The idea behind OOP is that attributes can only be accessed through methods provided in the class. The direct path to the data is unavailable. Attributes are referred to as ‘private’. The methods to access the data are made available to programmers, so these are ‘public’. The feature of combining attributes and methods together in a single class is known as encapsulation . Encapsulation supports information hiding through the combining of private properties and public methods into a class, ensuring private properties are only accessed/altered by calls to public methods. It also supports implementation independence, which is the use of methods of a class without needing to know how it is implemented. Even if the underlying implementation were to change, it does not matter to the user. In a class diagram, negative prefix denotes ‘private’ and positive prefix denotes ‘public’. Hence, data should be tagged with negative signs to signify that they are private attributes and methods should be tagged with positive signs to signify that they are public methods. 6
VJC/H2Computing/9569 Person -name -mobile_no +constructor() +get_name() +get_mobile_no() +set_mobile_no() +set_mobile_no() When designing a class, we need to think about the attributes we want to store and the methods we need to access the data. The methods used to get attributes of the object are usually referred to as getters . Any attributes that might be updated after instantiation will need subroutines to update their values. These are referred to as setters . Some attributes get set only at instantiation. For attributes that are designed not to be changed, do not need setters. Hence, setters are provided only for attributes values that are to be changed. 7
VJC/H2Computing/9569 Back to the idea of private attributes, they should always be declared as ‘Private’. One way of declaring an attribute as private is to put an underscore before an attribute name. However, in
Content continues in the PDF. Download PDF
Related notes
- NYJC 2026 Prelim P2Exam Papers · 2026
- NYJC 2026 Prelim P1Exam Papers · 2026
- DHS 2026 Y6 H2 Computing Prelim Paper 2_finalExam Papers · 2026
- ACJC 2026 JC2 Computing Prelim Paper 2 (Practical)Exam Papers · 2026
- 2026_NJC Prelim_Computing_P2.pdfExam Papers · 2026
- 2026_JPJC_Computing_Prelim_P2_finalExam Papers · 2026
- 2026_JPJC_Computing_Prelim_P1_markschemeExam Papers · 2026
- 2026_JPJC_Computing_Prelim_P1_finalExam Papers · 2026
- 2026 ACJC Prelim Computing Paper 2Exam Papers · 2026
- 2024 ACJC Computing PromoExam Papers · 2024
- 2023 ACJC Promo QPExam Papers · 2023
- 2022 ACJC Computing Promo Paper 2Exam Papers · 2022
- See all H2 Computing notes

