The Basics of OOP
If you examine the previous example closely, then you'll see that all of these objects have two traits in common:
- Each object has its own properties.
- Keyboards have keys. Keyboard
A
might have 30 keys and keyboardB
might have 40 keys. - Monitors have heights and widths. Monitor
A
is 32 inches long and monitorB
is 55 inches long.
- Keyboards have keys. Keyboard
- Each object can take some actions.
- A keyboard can send a signal to a computer and enable/disable
numlock
. - A monitor can show you frames and adjust brightness/contrast.
- A keyboard can send a signal to a computer and enable/disable
Generally, we can say that each object has two things:
- Properties
- Methods
Formal Definition of OOP.
Wikipedia says that "Object-oriented programming (OOP) is a programming paradigm based on the concept of 'objects', which can contain data and code: data in the form of fields (often known as attributes or properties), and code, in the form of procedures".
If you understood the monitor and keyboard examples, then you can easily relate to this definition now. Here "objects" are the keyboard and monitor; the "data" is the height, width, number of keys, etc.; the "code" is sending signals, displaying images, and so on.
This is the basic idea of what each object in an OOP design should have. Later, we will see that we can use instances
of these objects to know certain properties. For example in Python:
xxxxxxxxxx
## Suppose a keyboard is an object we have defined earlier
# Print a property : how many keys does this keyboard have?
print(keyboard.num_keys)
# Do an action : send signal to computer
keyboard.sendSignal('A')