Table of contents
No headings in the article.
P.S: I am doing this because my accountability partner has put a gun to my head and forced me to write. Ladies and Gentlemen, this is the result of thinking about objects all day long
Thinking in Objects
At our technology class at #MEST, our fellows made it clear that Object Oriented Programming is not just a programming style but a way of thinking, so it was important for me to learn how to think in objects first before learning object programming languages
But what does it mean to think in objects
Thinking in objects is a method of programming where groups of variables and functions are combined into a unit, which is then seen as an object. As an object, it has behaviors and attributes. what we call methods and properties in coding
and then you hear the very common example; An object is a Car ๐, and methods are the things a car can do, and properties are attributes a car possess
Is that really enough to start thinking about objects?
How do you translate that into programming? How do you think in objects when you code?
- Identify similar variables and group them together
- Look out for the relationship and hierarchy amongst a collection of information
- Identify data that are like to change based on input from the user
A simple example is the code below;
You can see here that Nneka & Amina are objects of the class "HumanBody", when they are instantiated, they possess characteristics: skin_color, hair_color, blood_type that are unique to them.
It is easy to see humans as objects, because we can quickly see our individuality by looking at them. It might be a bit difficult to see data as objects. we would have to understand how we represent objects using classes
Classes
It is easy to see the relationship between classes and objects using food. Every food we consume has a place in the food class.
This food class helps us group similar foods with the same properties and similar functions in the body. Every class in OOP is given a name, just like the food classes (carbohydrates, protein etc)
A class is a blueprint from which you can make an object.
Functions
If someone ask you to make a great meal for them, you might ask them:
- What properties do you want your food to have?
- What functions will it have on the body; should it give energy like carbohydrates or should it build muscles like protein, using this they are able to find
Methods
Going with the example of food classes, there are certain things that certain classes of food can do, these functionalities that are specific to a class of food are called methods. Methods are operations classes perform in OOP
Properties
Lets say, this friend decide He wants to eat Jollof rice ๐, a type (object) of food in Carbohydrates (class). Carbo-hydrates are Carbo (carbon) and hydrates (water). All carbohydrates these elements. these elements in OOP are called properties: properties are data that defines a class
Attributes
The qualities unique to an object are called attributes of an object. To instantiate a Jollof rice, we sort of like define it, and specify how much carbon and water our beautiful jollof rice ๐have. lets say our jollof rice contains 75% Carbon and 25% water.
Let's run through everything we have learnt from the beginning again.
Object-Oriented programming is a programming philosophy that allows us define codes as interactive objects.
Objects are grouped into classes, which is a blueprint from which several objects can be created.
These classes contain properties that we call variables, and perform some functions we call methods. When we instantiate an object we specify unique properties of the class it has. those unique properties are called attributes
Now you know this, can you think of ways you can actually implement object programming in coding. Describe some objects around you, what properties do they have and what actions can they carry. Lets write some code!
In a future post we would talk about the principles of Object-Oriented Programming.
share your thoughts, questions and feedback here as a comment or you can shoot me a DM on Twitter