What is Object Oriented Programming?
When you begin your journey into coding, you will read a lot about the concept of object oriented programming and how it revolutionized the way we code. Well, it’s not that new, but it’s a VERY important subject. You can choose not use OOP design when you program, but it’s just the way things are done now. Let me explain it in a visual way. Let’s say we’re making a game that uses many cars. There are hundreds of different cars out there so we need to reflect that in our game. In the old days, you would write the code to define every single car: color, make, model, fuel type, etc… You would need to rewrite all of this every time you needed a new car produced in the game, even if the cars were similar. That’s a lot of duplicated code. One of the golden rules of programming: never write duplicate code. It wastes time and is prone to errors. If all cars share some characteristics, there must be a simpler way to create them. We can create a single generic car class. It will contain the characteristics that all cars share. We can instantly generate an object of the type Car, as many as we want. Look at this pseudo code:
class Car ( ){
color;
make;
model;
fuel type;
}
myNewCar1 = new Car( green, Ford, Mustang, gasoline);
myNewCar2 = new Car( red, Toyota, Prius, electricity);
myNewCar3 = new Car( blue, Volkswagon, Golf, diesel);
I just created three new cars with minimal effort. Each car object was created from the generic Car class. Of course, this is a simplified example. Most cars are unique and we need more characteristics. We can derive subclasses of the Car class. They inherit all the characteristics of the Car class, but we can add additional things. For example, we can make three subclasses of Car: RaceCar, SUV & Compact. Each subclass inherits the same base characteristicts, but each also has some unique elements.
class Car ( ){
color;
make;
model;
fuel type;
}
class RaceCar extends Car (){
TopSpeed;
Rank;
}
RaceCar Lotus = new RaceCar( green, Lotus, Esprit, RaceFuel, 195 mph, 2);
Like everything in the real world, it gets more complex but hopefully you get the general idea. OOP behaves very similarly across all popular languages, so once you get the hang of classes, subclasses and inheritance you can appreicate the simplicity of OOP. For a more in depth explanation check out this page at Code Project.