- Published on
OOP Mastery - Theory 01: What is OOP?
- Authors
- Name
- Dan Tech
- @dan_0xff
The OOP Mastery series will provide complete theoretical and practical knowledge so that you can grasp OOP and have a quality project that is an Android Application.
OOP is an extremely important area of knowledge for any programmer. Whether you are going in the direction of Web, Backend, Mobile, or Data, AI development... OOP will always be with you. Mastering OOP will help you write quality code that is highly reusable.
I wish you success with the knowledge in the OOP Mastery course by @dantech0xff
The world of programming before OOP
The Programming profession has gone through many revolutions and changes to arrive at a method that is the basis and core for all current software - Object-Oriented Programming. In this exciting topic, let me introduce you to the programming methods that our predecessors used before the advent of Object-Oriented Programming (OOP).
Imperative Programming
Imperative programming can be seen as the simplest and most primitive programming method.
In this development phase, a program will consist of Input (input data) - Sequence of Processing Commands - Output (output data). The programs in this phase are mostly software used to calculate math operations and formulas to help scientists find answers faster. These programs have helped the science fields related to mathematics make great breakthroughs because they save experiment and calculation time (In fields related to mathematics: Physics, Chemistry, Astronomy, etc.)
A program written using Imperative Programming can be visualized as follows:
: Program A
- input
- Command 1 processes input -> output 1
- Command 2 processes output 1 -> output 2
- Command 3 processes output 2 -> output 3
- output
/*In reality, the program will contain more complex logic (loop, if - else, ...) */
You can see that with this approach, programs become specific to each problem. The code commands written are not reusable between problems.
At this time, people invented a new programming method -> Functional Programming
Functional Programming
In the Functional programming method, programmers basically still compile software based on designing command sequences for specific problems. However, when approaching functional programming, programmers will choose to handle problems in a general-to-specific direction. This will help group reusable logic into functions and share them between multiple problems.
This improvement helps reduce resources spent on repetitive tasks, and they invented libraries that make software design and execution easier.
You can think of libraries like math.h - which supports the reuse of logic for mathematical operations that need to be used multiple times in the calculation process.
A program written using Functional programming can be modeled as follows
: Program A
include library-1
include library-2
declare self-function-1
declare self-function-2
- input
- library-1$function // describes the use of library-1's function for a purpose
- library-2$function // describes the use of library-2's function for a purpose
- self-function-1 // describes the use of self-function-1 for a purpose
- self-function-2 // describes the use of self-function-2 for a purpose
- output
/*In reality, the program will contain more complex logic (loop, if - else, ...) */
In this approach, the program has become more general and more reusable. This is a better approach than the procedural approach in the previous section.
As society develops, social needs increase - people create more and also demand higher things. Software at this time is no longer just simple calculations - at this time software becomes a system of solutions for people. Organizing programs through functions is no longer suitable for social needs.
Software development at this time also requires more people, and more tasks are divided. To optimize the resources of programmers, they came up with a new approach: Object-Oriented Programming - by isolating and objectifying the features in the program to make code development and reuse easier.
The birth of Object-Oriented Programming (OOP)
Object-Oriented Programming (OOP) is an approach to problems based on the concept of "Objects". OOP will model software into entities (either tangible or intangible); the entities will always have 2 basic forms of resources: attributes/properties and behaviors - methods.
- Attribute: Are the values that the object holds, which can be updated and changed over time.
- Design software to draw a window on the computer screen with a width of 200px, a height of 80px, a gray window border, and a white background.
- In this specification, we can design a Window object with the following attributes to serve the program
- width: 200px
- height: 80px
- borderColor: GRAY
- backgroundColor: WHITE
- Behavior - method: These are the behaviors that the object can perform. Performing methods can change the value of the attributes.
- Inheriting the above specification, the window now has the ability to resize
- In this specification, we can design a Window object with the following behaviors to serve the program
- function draw() -> used to display on the computer screen
- function setSize(width, height) -> used to update the width and height values of the window
After having the specifications and Object-Oriented solutions for a program, we proceed to implement it in the source code.
class Window(private var width: Int, private var height: Int) {
fun draw() {
println("Draw window $this with width: $witdh, height: $height")
}
fun setSize(w: Int, h: Int) {
width = w
height = h
}
}
fun main() {
val w = Window(720, 1280)
w.draw()
w.setSize(1280, 720)
w.draw()
}
Core values of Object-Oriented Programming (OOP)
Object-Oriented Programming was created to help solve many problems in modern software development.
In the modern world, it changes very quickly. A modern software must serve many purposes. It must have high flexibility but also ensure stability. OOP is an excellent solution for these 2 problems.
By mastering the Characteristics of OOP, you will better understand what Flexibility and Stability are in software development and have a suitable way to build future projects for your career. Read the next Theory and practice sections in this series of articles to learn more!