HCI 9. Object Oriented Programming
Uploaded by adrianwang2003 · 28 May 2024
Preview
Hwa Chong Institution H2 Computing 1 9 Object Oriented Programming Learning Outcome 9.1 Procedural and Object-Oriented Programming 9.1.1 Procedural Programming : Writing programs made of functions that perform specific tasks - Procedures typically operate on data items that are separate from the procedures - Data items commonly passed from one procedure to another - Focus: to create procedures that operate on the program’s data 9.1.2 Object-Oriented Programming : focused on creating objects Object: entity that contains data and procedures - Data is known as data attributes and procedures are known as methods - Methods perform operations on the data attributes Encapsulation: combining data and procedure into a single object Fundamentals of Object-Oriented Programming Define and understand classes and objects Understand encapsulation and how classes support information hiding and implementation independence Understand inheritance and how it promotes software reuse Understand polymorphism and how it enables code generalisation. Exclude: method overloading and multiple inheritance
Hwa Chong Institution H2 Computing 2 Data hiding: object’s data attributes are hidden from code outside the object and access is restricted to the object’s methods - Protects from accidental corruption - Outside code does not need to know internal structure of the object An Everyday Example of an Object - Data attributes: define the state of an object o Example: clock object would have second, minute, and hour data attributes - Public methods: allow external code to manipulate the object o Example: set_time, set_alarm_time - Private methods: used for object’s inner workings 9.2 Class vs Instance/Object Class: code that specifies the data attributes and methods of a particular type of object o Similar to a blueprint of a house or a cookie cutter Instance: an object created from a class o Similar to a specific house built according to the blueprint or a specific cookie o There can be many instances of one class The housefly and mosquito objects are instances of the Insect class
Hwa Chong Institution H2 Computing 3 9.3 Class Definitions set of statements that define a class’s methods and data attributes - Format: begin with class Class_name: o Class names often start with uppercase letter - Method definition like any other python function definition o self parameter: required in every method in the class – references the specific object that the method is working on 9.3.1 Initializer Method Also called class’s constructor, this method automatically executed when an instance of the class is created - Initializes object’s data attributes and assigns self parameter to the object - Format: def __init__ (self): - Usually the first method in a class definition (coin.py) import random # The Coin class simulates a coin that can be flipped. class Coin:
Content continues in the PDF.
Related notes
- VJC Chapter 21 SQLite with PythonNotes/Practices · 2025
- VJC Chapter 23 Web Applications PrinciplesNotes/Practices · 2025
- VJC Chapter 10 RecursionNotes/Practices · 2025
- VJC Chapter 20 SQLNotes/Practices · 2025
- VJC Chapter 16 Hash TableNotes/Practices · 2025
- VJC Chapter 22 NoSQLNotes/Practices · 2025

