Object Oriented Programming: Classes and instances BEHIND THE SCENES!


Object Oriented Programming: Classes and Instances


Hi! Welcome to this section! First of all, congratulations! You’re already halfway through the course. You should be very proud of yourself :-)
In this section we are going to start talking a little bit about classes, what they are and how they can represent real objects.
You will even learn that YOU are an example of Object Oriented Programming (OOP) and you didn’t even know it!
So… Let’s get started!

  • Classes are like blueprints, we use them to represent an abstract notion or idea of how we describe real world objects according to certain criteria.
For example, House is the notion we have of a structure where people live that has bedrooms, bathrooms, floors, roofs, ceilings, and so on… they share some characteristics, but they don’t necessarily have to be exactly the same for us to consider them houses.
This is what we would represent as a CLASS, the abstract notion of a house.
  • Now, if we refer to a specific house, concrete examples, like my house, your neighbor’s house, a house down the street, we are talking about specific or concrete examples of the abstract notion of what a house is. We call these examples INSTANCES.
In the tutorial we will go through several examples of these ideas, and at first they may seem too abstract, but I promise you as we go deeper into some examples, it will become much more concrete.



LEARNING CLASSES THROUGH AN EXAMPLE!

Here I present you the example we will be working with, the one I presented previously about Houses.
(Don’t worry at the moment if you don’t understand all the code in the example, we’ll break it down by small components and discuss every aspect in detail).


  • The first line in the example is where we declare that we want to create a class that is named House and that inherits from object (don’t worry about this inheritance aspect right now, you will study it in lectures later on and we will discuss it in a later tutorial as well





__init__()

  • Now, we first find a very peculiar function with a notation we haven’t seen before. The keyword def is familiar to us, but using double underscore before and after the function’s name is new to us. You must use the name init for this method if you want it to retain all the properties we will discuss.
  • You can think of __init__( ) as the function that creates a blueprint for creating instances of a class. This function runs when an instance is created and gives it the properties we have define in this “blueprint”



STEP BY STEP

Let’s see what happens with the function __init__( ) when we create an instance.
  • First, we must create the instance itself by using the syntax depicted in the slide.
  • Then, the function __init__() will be called and the arguments we passed in parenthesis will be added to the function scope just like we studied in previous functions.


  • We then use the variables added to the function scope and set them as the values for the instance’s attributes (right hand side of = )
  • We assign this value to an attribute we’ve created for the instance by using:
self.attributeName = variableName

Not all attributes need to rely on a variable passed as an argument! Some may already have a value predefined and all instances will initially have the same value for that attribute.




SURPRISE! YOU ARE OBJECT ORIENTED PROGRAMMING!

And now we come to the big surprise I promised you at the beginning… YOU ARE AN EXAMPLE OF Object Oriented Programming AND YOU DIDN’T EVEN KNOW IT! :-)
  • All humans share some criteria and categories of characteristics and can perform certain actions as individuals that can affect these initial attributes (these are called methods, we will discuss methods in the next tutorial).
We will use this example to show how in depth how instances work

INSTANCES IN DEPTH

We’ve discussed the class as a whole, so now let’s talk about INSTANCES.
  • Instances are specific and concrete examples that belong to a class.
  • In the case of the class Human, you are an instance of this class because you share all the attribute categories for the class, you follow the “blueprint” defined by the class and you can perform all the actions (methods) from the class.
BUT… and this is a very serious BUT that you will have to remember to fully understand Object Oriented Programming….
  • Instances have their own individual values for each one of these attribute categories defined in the class.
You may have a specific height, weight, hair color, and so on for every attribute defined in the Human class, but another person will not have the same values for these attributes as you.
  • This is the point of Object Oriented Programming, it provides the blueprint to create individual instances that share the same structure but have their own set of characteristics. They can perform operations on their attributes, change them, and other instances won’t be affected.

SO FAR... : )

So far we’ve covered naming a class, the __init__() method and the concept of an instance and how to create them. In the next tutorial we will discuss methods and how they are related to classes and instances. STAY TUNED! :-)



NOTE: Check out this link to learn more on the difference between an object and an instance :)

Quote from the link:
A blueprint for a house design is like a class description. All the houses built from that blueprint are objects of that class. A given house is an instance

SECOND PART OF THE TUTORIAL!

Hope this helps!
If you have any questions, please post them on the forums, community TAs and your classmates will always be there to help you!
Estefania.
P.S = images of characters and houses used in the diagrams were taken from pixabay.com under public domain.

Comments