In this series, you will be introduced to the enormous domain of Object-Oriented Programming
. Object-Oriented Programming (OOP) is not a library or framework - it is a programming paradigm.
Many popular languages support the OOP paradigm (e.g., Java, C++, Python) and many languages don't (e.g., Erlang, Haskell, ML, C, Scheme, Lisp). However, OOP is a fundamental concept in computer science. Furthermore, OOP is frequently asked about in technical interviews. In an interview, you may be asked to:
- Code an OOP solution
- Draw a UML Diagram to solve a system design problem
- Explain the class hierarchy of a program architecture
- Examine differences between composition and association.
If you do not have any computer science background, then these lessons will be vital for your future education in computer science. So let us begin!
What is Object-Oriented Programming?
Object-Oriented Programming
is a programming paradigm (think of it as a methodology/writing style) in which you think of everything as real-life objects.
What does an object in real life look like? Think about what you have in front of you right now. You are scrolling through this lesson with a mouse. You are looking at a monitor. You are typing on the keyboard. All of these things are objects. What do they "have"?
- Monitor:
- A width and height.
- A display that can show images/videos to you.
- Mouse and Keyboard:
- Buttons with names or numbers.
- A signal which is sent to the computer when you press a button.

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')
Build your intuition. Click the correct answer from the options.
What are the two attributes of an object in OOP?
Click the option that best answers the question.
- Objects and statics
- Properties and Statics
- Properties and Methods
- Statics and Methods
Let us see a real OOP example now.
Defining classes
Enough theoretical knowledge! We will come back to theories once we are comfortable writing in the OOP style. For years Java
has been the best programming language to learn about OOP, so we will be using Java for this example. AlgoDaily, however, tries to be as language-agnostic as possible, so we will also be adding some other languages such as Python and C++ for completeness.
Java has the keyword class
which defines a class. A class can be thought of as a blueprint for an object. For our keyboard and monitor example, a blueprint of keyboards and monitors will tell us what properties exist for a keyboard and monitor. For example:
- A keyboard must have a number of keys
- A keyboard must be able to send data via wired or wireless connection.
- A monitor must have a width and a height
Let's define the keyboard class in the simplest way possible. Create a file named Keyboard.java
and put the below text there.
As you can see, there are two properties (num_keys
and chars
) of the class
and 1 method (sendSignal
).
xxxxxxxxxx
public class Keyboard {
int num_keys;
char[] chars;
void sendSignal(char key) {
System.out.println("Signal sent : " + key);
}
}
Instantiating objects
After running the above code, you will not find any output. Rather, you may get an error message saying "No Main Class Found". This occurs since all Java
programs must have a main
method where the program will start. The Keyboard
class, however, should not have a main
method, so we will create another class named Main
inside a new file called Main.java
and run it.
We somehow need to use that class (blueprint) file to create different kinds of keyboards. The process of creating a real object from a class is called object instantiation. In Java, we can instantiate an object using the new
keyword.

xxxxxxxxxx
public class Main {
public static void main(String[] args) {
// Instantiating an Integer
Integer num = new Integer(10);
// Similarly Instantiating a keyboard
Keyboard keyboard = new Keyboard();
// Writing to a properties of that instance
keyboard.num_keys = 26
// Accessing properties
System.out.println(keyboard.num_keys);
// Calling methods
keyboard.sendSignal('a');
}
}
Let's test your knowledge. Fill in the missing part by typing it in.
How do you instantiate a class named Blosom
and put it in an already declared variable named blsm
in Java? (Write without any spaces)
Write the missing line below.
Examples in other languages
In most modern programming languages, OOP is natively supported, so there is most likely a keyword named class
and/or new
in your favorite language. Most languages also contain some simplified notation for classes. For example, you can access a property or call a member function of an object using the .
(dot) operator. An additional benefit of learning the concept of OOP is that it will help you write code with good practices in many modern languages.
We will be showing you the most common C++
, Python
, and Javascript
languages with the above example.
Below is the C++ code for the above example. Java recommends that you create each new class
in a separate file or they cannot be public (more on this when we discuss encapsulation in a later lesson). But in C++, you can create any number of classes in the same file. Also, your main
method is outside of a class. This is because C++ is based on a Structured Programming Language which is why it has a lot of features that make the language less OOP supported.
1#include <iostream>
2
3using namespace std;
4
5class Keyboard {
6public:
7 int numKeys;
8 char *keys;
9
10 void sendSignal(char ch) {
11 cout << ch << endl;
12 }
13};
14
15int main() {
16 Keyboard keyboard;
17 keyboard.numKeys = 26;
18 cout << keyboard.numKeys << endl;
19 keyboard.sendSignal('a');
20}
Below is Python code for the same Keyboard. Python is a loosely typed language and is a little farther from OOP compared to C++ and Java. The syntax might seem weird at first. Python always needs a default value to be set explicitly for classes. In Java and C++, all data types have an implicit default value (e.g., 0 for integers and "" for strings).
1class Keyboard:
2 # We will talk about this weird __init__ method in next lesson when we introduce constructors.
3 def __init__(self):
4 self.numKeys = 0
5 self.chars = []
6
7 def sendSignal(ch):
8 print(ch)
9
10# This is similar to main method
11if __name__ == '__main__':
12 keyboard = Keyboard()
13 keyboard.numKeys = 26
14 print(keyboard.numKeys)
15 keyboard.sendSignal('a')
Below is the same implementation in Javascript.
1class Keyboard {
2 constructor() {
3 this.numKeys = 0;
4 this.chars = [];
5 }
6 sendSignal(ch) {
7 console.log(ch);
8 }
9}
10
11var keyboard = Keyboard();
12keyboard.numKeys = 26;
13console.log(keyboard.numKeys);
14keyboard.sendSignal('a');
Conclusion
This is just a warm-up lesson for the upcoming series of Object-Oriented Programming
. Trust me, we haven't even scratched the surface. In the next lesson, we will go through the 4 fundamental rules of OOP and get to know them through coding examples. After completing the whole series, you will know almost everything about OOP and will be prepared for any OOP questions in a technical interview.
One Pager Cheat Sheet
- This series introduces you to the concepts of
Object-Oriented Programming
, which is a fundamental concept in computer science and is frequently asked about in technical interviews, including questions about coding, UML Diagrams, class hierarchy and composition versus association. - Object-Oriented Programming views reality as made up of
objects
, each with its own properties and behaviors that can be described using attributes and methods. - OOP is a programming paradigm based on objects which contain data and code, allowing them to have properties and methods which can be used with
instances
of the same object. - An object in Object-Oriented Programming has
properties
andmethods
. - We can define a class using the
class
keyword, which acts as a blueprint for an object, consisting of properties and methods, as illustrated by a simpleKeyboard
class example in Java. - We need to use the
Keyboard
class to create different kinds of objects by instantiating them with thenew
keyword. - To create an instance of a class and assign it to a variable, the syntax
variable = new ClassName();
must be used. - The OOP concept of a
class
and associated functions are supported in most modern languages such asC++
,Python
, andJavascript
. - Completing this series will enable you to understand the fundamentals of
Object-Oriented Programming
and prepare you for OOP questions in technical interviews.