Classes and Objects¶
1. What is Object Oriented Programming, and why do we need it?¶
We come across a lot of different objects in our daily life. Each object has its own properties, some features that define it.
Lets take a pen for example. What are the properties of a pen? Its colour, its size, its kind (ball-point, fountain-point, gel-ink) and maybe the name of its owner.
Another example is a textbook. A textbook has a size, it has a subject, it has a length (the number of pages) and it has some information inside of it. Now the information inside a textbook is organised into named chapters.
For example, a maths text book might contain chapters like "sets", "trigonometery", "calculus" and so on, and if you want someone to read or go through a chapter, you'd say something like "go through the chapter on calculus".
Now imagine that you are a programmer who wants to write code that describes a pen, or a textbook... how could you go about writing code that expresses these properties discussed above?
You might try writing code that looks similar to this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
- What if you wanted to make an unknown number of pens? How would someone know how many variables to declare?
- What if you had a more complicated object with 100 properties? Would it be feasable to manually declare 100 variables for every object that you might need to create?
This is where classes come into the picture. So far we have learnt about the primitive data types in python, primitive meaning that they are in-built, simple data types which python provides to us. Now we are moving on to custom data types, data types that are defined by you, the programmer!
2. What are classes, and why do we need them?¶
So now, we want to create our own data types, a data type that would allow us to describe a pen, or any other object effectively, using code. This is exactly what a class allows us to do!
A class is basically a blue-print for creating an object, it tells us the defining properties of the object, and it also tells us what functions the object can perform. Following the class blue-print allows us to create "instances" of that class.
An object of a class, the realisation of the blueprint, is known as an instance of the class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
|
3. Classes, conventionally¶
All programmers mutually agree to follow some rules, called conventions that are not necessary, but nice to follow while writing classes and make your code more readable to a usual programmer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Write a class that describes a bicycle object
Which properties should a bicycle object have?
- Colour (red, blue, white, etc)
- Material (steel, aluminum, plastic, wood, etc)
- Size (small, medium, large)
- Height of the seat (in m)
- Gear ratio (1, 2.5, 4, etc)
- Diameter of the wheels (in cm)
- Does it have a basket
- Does it have a Bell
What functions should a bicycle have?
- Change gear ratio
- Adjust seat height
Write a class that describes a bicycle object
Which properties should a bicycle object have?
- Colour (red, blue, white, etc)
- Material (steel, aluminum, plastic, wood, etc)
- Size (small, medium, large)
- Height of the seat (in m)
- Gear ratio (1, 2.5, 4, etc)
- Diameter of the wheels (in cm)
- Does it have a basket
- Does it have a Bell
What functions should a bicycle have?
- Change gear ratio
- Adjust seat height
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
4. What makes classes so good?¶
- Reusability: The same class can be used to make as many objects as you want
- Modularity: The code becomes incredibly modular, and it is easy for a programmer to debug the code in case there are any bugs
- Clarity of code: Due to the code being modular, it is easier for others to read and understand the code
- Better organisation: The data can be clearly and neatly organised for more complex objects
-
Data Abstraction: This is the process of hiding the implementation details from the user, allowing them to focus on the functionality instead.
Example: you don't need to know a smartphone works internally to be able to use it. The details about its circuits, its workings are hidden from you, the user! Instead, the smartphone provides you with functions (call, message, surf the internet) only.
Example in python: The functions like
math.sin()
andmath.cos()
can be used to find out the sine or cosine of an angle, but they do not tell you how the calcualtion is actually done. Those implementation details are hidden from you, the user and you only need to focus on the functionality!
5. An object can also have other objects as its properties¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|