Kodeclik Blog

Objects in OOP

Object oriented programming (OOP) is a style of programming in which your code maps to objects found in the real world. Specifically, an object denotes a collection of variables and functions that are encapsulated to denote objects found in the real world. Because your code maps to the real world it logically breaks down your program into manageable chunks.

What are some OOP languages?

Smalltalk is one of the earliest object oriented programming languages where everything is an object. C++ and Java are newer object oriented programming languages. Smalltalk is a "pure" object-oriented programming language whereas C++ and Java have both object-oriented and non-object-oriented elements. This means that it is impossible to write a program in Smalltalk without subscribing to the object oriented programming model.

Example: Minecraft and other computer games

Let us imagine Minecraft using the object oriented programming paradigm. Every character in Minecraft that has some role or functionality is an object. So Steve and Alex, the main players are objects. A pig is an object; in an environment you can have multiple pig objects. An Enderdragon is also an object.
Each object has some attributes stored via variables. For instance, the location Steve is at can be stored in a variable that is encapsulated inside the Steve object. Similarly other objects have their locations. Objects will also have functions that enable them to perform operations, such as move, harvest, and jump. Different objects will have different functions, i.e., not all objects have the same capabilities.

Example: Your Car

Cars nowadays are heavily computerized systems and all the parts, sensors, instruments, and actuators can all be viewed as objects. For instance your car’s speedometer is an object. The current speed is stored in a variable inside this object. The accelerator pedal is an object. It stores the amount of pressure being applied on it. Tires, engines, and motors are objects too. Objects communicate with each other via messages. So when you press the accelerator it sends a message to the engine which sends a message to the tires. In fact, the car itself can be viewed as an object that is made up of other objects.

Objects have state

In examples of OOP, it is important to note that the variables in an object often denote the “state’ of the object. For instance, the speedometer object has a speed value which denotes the current rate at which the car is moving. Similarly, the fuel gauge (or charging level, if you have an electric car) has a variable that denotes the level of emptiness of the tank or the % charged of the car. As functions are used (e.g., the acceleration pedal is pressed or as the car is driven long distance) the state of these variables change.

Example: your community hospital

In a hospital, objects can refer to patients, doctors, surgeons, nurses, and laboratory technicians. Patient objects will have name, age, condition for whom they were admitted. Doctor objects will have name, qualifications, and speciality. Laboratory technicians will have name, qualifications, and speciality. The “states” of the objects denote how these people are functioning in the system. For instance, a patient’s state could be “admitted”, “in surgery”, or “discharged”. A doctor’s state could be “on duty”, “on leave” etc.; a surgeon’s state could be “in operating room”, “consulting with patients”, and so on.

How to conceptualize objects

If you are trying to code up an application using object oriented programming, look for nouns in your application and whether they have state and whether they have important functions to perform in the application. For instance, consider your Roomba vacuum floor cleaner. The Roomba has many states: charged/uncharged, empty/partially full/full etc. The Roomba has many functions, e.g., “clean the room”, “dock and charge”, etc. This is how you can conceptualize Roomba as an object. If you have two Roombas in your home, each of them is an object.

What are advantages of objects and OOP?

Object oriented programming has many advantages. First, thinking of code in terms of objects creates a structured decomposition of the world and localizes functionality. So if we need to update the code for a speedometer in a program modeling a car, only the object corresponding to the speedometer needs to be changed. Second, by using object-oriented programming, we create re-usable components that can be interchanged and intermixed in different ways. Thus OOP supports “code reuse”. This is very common in large modern software repositories where objects created by one group can be reused in programs written by others.
If you liked this blogpost, checkout our article on sequences.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

Copyright @ Kodeclik 2024. All rights reserved.