Object Oriented Programming

ITEC 4400/2160 Python Programming for Data Analysis,
Cengiz Günay

(License: CC BY-SA 4.0)

Prev - Recursion, Next - Working with vector data

What is object oriented programming?

  • Analogy to way we interact with objects in real world
  • A component-oriented approach
  • Promotes reusability

OOP: encapsulation and data abstraction

  • Two of the fundamental pillars of OOP
  • Data and functions (methods) go together
  • Unimportant details of code are hidden

OOP in Python

  • Constructors
  • Class attributes
  • Instance attributes
  • Calling class methods
  • Magic methods

Class activity: make your own class!

  • Teams of 2, pick a marker
  • Write code on a board that includes:
    • A class with a constructor
    • One or more method functions
    • Make sure to have some class attributes that serve a purpose
    • Also have instance attributes
    • Create several instances of the class and call its methods to demonstrate

Class activity: now make it on Repl.it

  • No teamwork; create a file with your name and put your class in your own fork following the template below:

Inheritance and Polymorphism

  • The last two fundamental pillars of OOP
  • Inheritance: subclasses inherit methods and attributes
  • Polymorphism: all subclasses can be used interchangably, regardless of implementation details

Class activity: inherit!

  • Follow instructions by forking it.

Class activity: polymorphism

  • Teams of 2, pick a marker
  • Write code on a board that includes:
    • A parent class and 2-3 child classes
    • Create parent class method to be overloaded in the children
    • Implement different versions of method in child classes
    • Demonstrate calling the method in children object instances to show different outcomes
    • Must have a meaning!
Home