Fundamentals of Object-Oriented Programming
1. Fundamentals of Object-Oriented Programming
Welcome! I'm Jake, and I'll be your instructor as we explore the world of object-oriented strategies and techniques using Python. But first, let's recall the fundamentals of object-oriented programming.2. Defining a class
To create a Python class, we'll use the class keyword and the name we'd like to give the class. Typically, the first method defined in a class is the __init__ method. __init__, also known as a constructor, is called when a new class object is created. It takes self as it's first parameter, along with any additional parameters that will be passed into the class. In Python, self refers to the current instance of a class, and is used extensively when building one.3. Instance attributes
Instance attributes are associated with a class object. They can be set and accessed within a class using self-dot-attribute, as well as once an object has been created using objectname-dot-attribute.4. Class attributes
Class attributes are not tied to a class object, but rather, the class itself. They're defined outside the constructor, and can be retrieved without a class object using the syntax classname-dot-attribute. Class attributes are typically used to store information that should remain the same for all objects of a certain class.5. Instance methods
Let's take a closer look at methods. Here, introduce is defined as an instance method. Instance methods require an object of a class to exist in order to be called. They take self as their first parameter, allowing them to access other instance methods and attributes.6. Class methods
Class methods are defined using the classmethod decorator. They can't access instance methods or attributes, but can be called without needing a class object to exist. Here, wake_up is a class method, and is invoked using the Person class itself.7. Inheritance
Inheritance allows for code to be reused between classes. Here, the Employee child class inherits all functionality of the parent class, Person. Inheritance can be thought of using an "is-a" relationship. In our example, Employee is a Person, so it makes sense for Employee to inherit Person. Once a class has been inherited, it can implement additional functionality, such as new attributes or methods.8. Inheritance
Here, an Employee object is created, called Lester. The introduce method inherited from Person is called, along with the change_position method defined in Employee.9. super()
Before, we used the class name and __init__ method to instantiate the attributes and methods of the parent class, Person. The super function can be used to do something similar. Below, we've rewritten the Employee constructor to use super, providing the name and age attributes to __init__. With super, we don't need to pass the self keyword to __init__.10. Overriding
When a child implements a method inherited from a parent in a new way, this is known as overriding. Here, the introduce method is overridden by Employee. The new method prints an updated greeting that includes both the employee_name and title.11. Overloading
Different than overriding, overloading allows for the behavior of a Python operator to be customized for a certain class. To determine if two objects of a class are equal, we can overload the equality operator using the __eq__ magic method. We'll take a closer look at overloading in the next lesson, but now...12. Let's practice!
... let's practice with a few exercises!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.