Object-Oriented Programming

Definition

  • Programming which is focused on creating objects
  • Objects are entities that contains data and procedures
  • Data is known as data attributes and procedures are known as methods
  • Methods perform operations on the data attributes

Benefits

  • Reusability: Programs can be assembled from pre-written software components that can be used in many different applications
  • Extensibility: New software components can be written or developed from existing ones without affecting the origin components

Encapsulation

  • Combining data and procedure into a single object
  • 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
Hiding Attributes
  • An object’s data attributes should be private
  • In Python, a name prefixed with an underscore (e.g. _sideup) is a commonly used convention to denote a private attribute or method
Accessor and Mutator Methods
  • All data attributes are private and provide methods to access and change them
  • Accessor methods: Return a value from a class’s attribute without changing it
    • Safe way for code outside the class to retrieve the value of attributes
  • Mutator methods: Store or change the value of a data attribute
Storing Classes in Modules

Classes can be stored in modules

  • Filename for module must end in .py
  • Module can be imported to programs that use the class

Class vs. Instance

Class

  • Code that specifies the data attributes and methods of a particular type of object

Instance

  • An object created from a class
  • There can be multiple instances of the same class

UML Class Diagram

  • Order of class, attributes and methods
  • ‘+’ means public, ‘-’ means private
  • Subclasses point towards the superclass

Inheritance

Definition

“Is a” Relationship
  • Exists when one object is a specialised version of another object
  • Object has all the characteristics of the general object plus unique characteristics
Inheritance
  • Used to create an “is a” relationship between classes
  • Superclass (base class): a general class
  • Subclass (derived class): a specialized class, an extended version of the superclass
    • Inherits attributes and methods of the superclass
    • New attributes and methods can be added

Polymorphism

  • An object’s ability to take different forms
  • Essential ingredients of polymorphic behavior:
    • Ability to define a method in a superclass and override it in a subclass → Subclass defines method with the same name
    • Ability to call the correct version of overridden method depending on the type of object that is used to call it
  • Its purpose is to allow different objects to be treated independently, such that when calling the methods of the same name, the programme is able to call the correct version of the method depending on the type of object

Classes

Superclass

  • The purpose of a superclass is to define common attributes and methods that can be shared by multiple related classes

Subclass

  • The purpose of a subclass is to
    • Inherit the properties and methods from the superclass
    • Specialise or extend them by adding new attributes or new methods to fit more specific needs