Skip to content

Learning Python Day: 58

Python Object Oriented Programming Course – Codecademy

Programming paradigm – a way to classify different programming languages and the unique features that they offer

Object-Oriented Programming (OOP) – ability to create programs around classes and objects

Breakdown:

  • representing a real-world entity (a dog) as a class with properties (name and age) and methods (bark)
  • these features make up the core of the OOP paradigm and allow us to build more intricate programs
  • Need to explore four core pillars of OOP

Four Pillars of Object-Oriented Programming

  • Encapsulation – bundle related data and behavior and limit data scope
  • Abstraction – hide complex logic from the user to make code more accessible
  • Inheritance – class can inherit all the functionality and attributes of another class
  • Polymorphism – idea that two or more classes can be of the same type and used interchangeably

Excercise:

  • Create a class, Employee
  • Define a variable, new_id and set it equal to 1
  • Each employee instance needs its own unique ID
  • Define an __init__() method
  • Inside __init__(), define self.id and set it equal to the class variable new_id
  • Lastly, increment new_id by 1
  • create a function to output the instance id
  • Inside the Employee class:
  • Define a say_id() method
  • Inside say_id(), output the string "My id is " and then the instance id
  • create 2 employees and have them give their ids
  • Outside of the Employee class:
  • Define the variable e1 and set it to an instance of Employee
  • Define the variable e2 and set it to an instance of Employee
  • Have both e1 and e2 output their ids
Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *