Welcome to the world of Object Oriented Programming.
In these next few lessons, we will give you a basic overview of what object oriented programming is.
In the real world, objects are anything where the characteristics are well defined - a cup, a table, a bag, etc. Each object has different characteristcs that make them easy to distinguish between. You know that a cup is different than a table, because they both have different characteristics.
Let's take an example of a restaurant.
Can you identify the objects involved in this scenario?
The objects involved are a restaurant, menu, waiter, order, food, and yourself. You can see that each of these objects are interacting with each other. Each object has its attributes, as well as functions that other objects can use to interact with each other.
Object oriented programming is a paradigm, or a mental model. It's not a programming language, but rather a way to design your software.
"Object oriented" means that you think of your software as a collection of objects and the interactions that happen between them. As we saw in the example of a restaurant, in real life, we can think of scenarios as objects interacting with each other.
By organizing our code in such a way, we can build code that can be easily reused. When we can reuse code, then we are keeping our code DRY (Don't Repeat Yourself) and maintainable. We are also forced to visualize what objects are involved in our program, and what the attributes, functions, and responsibilities are for each object, and how they should interact with each other.
Let's talk about cats.
If we think about cats, all cats share same characteristics but also differ in how they look, their age, and their given name.
All cats have:
All cats can:
We can organize this information into a class that we can name Cat
. Inside the class, we can have variables that represent the common attributes of a cat, and methods that represent the common operations that all cats can perform.
Each different cat is an instance of the Cat
class:
Let's take another example - cars.
All cars have:
All cars can:
The things that all cars have are the attributes. The things that all cars can do are the methods
.
Every car is an instance of a Car
class. The Car
class simply serves as a blueprint for creating new instances of cars.
In other words, the Car
class provides a blueprint: every car has a color, brand, max speed, and so on. However, every car also has different colors, brand, and max speeds. These individual attributes are different for each instance of the Car
class.
Let's think about the bill generator program that we wrote before. When we generate a bill, what are the objects that are involved?
Let's first talk about orders.
What does an order consist of?
It consists of...
An order also might consist of information like when the order was taken, who the order was taken by, and so forth, but for now let's just focus on these two pieces of information: the item of the order and the price of that item.
Let's look at an example:
Order 1
Order 2
Order 1
Here, we see three orders, each with different information. How can we represent these 3 orders in a consistent and understandable way in code? We'll explore how we can organize information into classes in the next lesson.