What is an object, or class? Let's use IceCream to demonstrate

by jj

Software Development Journey Overview

Follow this step-by-step software development journey to see real progress updates, challenges overcome, and practical experience.

Progress Updates (5 total)

Update #1: What is an object, or class? Let's use IceCream to demonstrate

Using objects (or classes in Python) is a simple, but powerful concept in programming, which seems to generate some confusion. Let's try to clear this up: We can think of a class as a repeatable template to build something. This class has properties that are unique to it, and classes make it easy to access these properties. Let's create a class called IceCream

Update #2: Constructing, or initializing, a class

Objects/classes always include a builtin function to set their properties. This builtin function, (__init__ in Python) will always be run once a class is called to be made. One of the most powerful aspects of a class is being able to to track all methods and properties in the class, by using a prefix. In Python, we use the "self." prefix, but most languages use "this.". Let's initialize three properties in our class: flavor, size, and cone.

Update #3: Creating an instance of a class

Like we mentioned, classes are just templates. This means, we can make as many copies of our IceCream class as we want. And what do you think we'll see if we print out the flavor of our IceCream instance? That's right, Strawberry!

Update #4: Making each instance unique

We can see where the real power of classes come in when we start passing custom properties into our class. Instead of the class having default values, let's make a strawberry and chocolate IceCream. We will pass these values into the initializer Both strawberry_ice_cream and chocolate_ice_cream can be used throughout your program now, and they will always be an instance of IceCream, but with those properties

Update #5: Making a method in IceCream class

Let's make a method called print_ice_cream_properties. It will just tell us some information about the type of IceCream we have. If we were to run the same thing for our chocolate_ice_cream, what do you think would happen?