DEV Community

Vıɔk A Hıƃnıʇɐ C.
Vıɔk A Hıƃnıʇɐ C.

Posted on • Updated on

You don't know OOP, the heater does♨️

Image descriptionOnce upon a time in an enchanted forest there was a magic box that contained a variety of objects with unique identities, states and behaviors. Each object knew its purpose, but could not perform a function on its own. On a very cold winter day, the resistor, an object that could heat, realized it needed the ability of energy to function. The battery, an object that could provide energy, offered the resistor the ability to inherit its energy, which allowed the resistor to heat. However, the resistor also needed someone to help disperse the heat it produced. The fan offered to help, but it also needed to inherit the battery's energy. Together, the objects created a larger class called "Heater."

When they started all the systems, they realized that the fan did not work. They discovered that the battery power functionality did not work for the fan because it used AC current, while the resistor used DC current. The battery, which could provide power to both objects, decided to implement an interface to allow each to implement its own functionality through polymorphism.

After solving this problem, they realized that they could not know the temperature at which the resistor was operating. The larger class, the Heater, asked the resistor if it had encapsulated the temperature and could not see or manipulate it. The resistor explained that it was designed that way and asked if that was a problem. The older class explained that he could control the fan and know its speed, so he needed to be able to control the temperature of the resistor as well. Finally, the resistor exposed its encapsulated properties, which allowed the senior class to function perfectly.

At some point, the battery died and everyone knew they were going to freeze. However, the electrical outlet, which had been frozen, offered to help and provided them with power. The senior class asked him how he could do this, and the electrical outlet explained that the senior class should implement power functionality through him and send charge to the battery. With the help of the electrical socket, the objects were able to survive the cold winter. 🤩

Detail enlargement

In terms of OOP concepts, a good implementation of encapsulation is observed in the resistor and the exposure of its encapsulated properties when needed. Inheritance was implemented when the resistor inherited the battery's ability to provide power and when the fan and resistor inherited power from the battery through the larger class. Polymorphism was used when the battery implemented an interface to allow the resistor and fan to implement their own functionality. Finally, the creation of a larger class to abstract the operation of several objects is noted.

Overall, the story is a good analogy for understanding the fundamental concepts of object-oriented programming, such as encapsulation, inheritance, polymorphism and abstraction_.

First, encapsulation refers to the protection of the data and behaviors of a class to prevent its direct access from other classes. In the story, the resistor had its temperature information encapsulated, which caused problems for the heater. However, by exposing its encapsulated properties, the problem was solved and the heater was able to function correctly.

Second, inheritance refers to the ability of one class to inherit properties and behaviors from another class. In the story, the resistor inherited the ability to power itself from the battery and the fan inherited the ability to disperse heat from the heater.

Third, polymorphism refers to the ability of different objects to respond to the same message in different ways. In the story, the battery implemented an interface to power the resistor and the fan differently, through polymorphism, to solve the problem that they both used different types of current.

Finally, abstraction refers to the ability of a class to hide the details of its implementation and present only the relevant aspects to the user. In the story, the heater class was presented as an abstract entity that included the resistor, fan, and battery, but hid the details of its implementation.

In summary, the OOP heater story is a good way to understand the key concepts of object-oriented programming. By giving it a more magical and enchanting tone, it makes it more appealing to the general public and makes it easier to understand these concepts. We hope this story will inspire more people to learn about object-oriented programming and improve their OOP skills. 😎🚀

Y entonces luego llegaron Batman y los Power Rangers y... to be continued!! 🤣

Top comments (0)