All of the previous lessons up to this point have one thing in common -- they have been non-object-oriented. Now that you have a basic handle on those concepts, we can proceed into object-oriented programming (OOP), where the real payoff is!
In traditional programming, programs are basically lists of instructions to the computer that define data (via variables) and then work with that data (via statements and functions). Data and the functions that work on that data are separate entities that are combined together to produce the desired result.
So what is object-oriented programming? As with many things, it is perhaps understood most easily through use of an analogy. Take a look around you -- everywhere you look are objects. Objects have two major components to them: 1) A list of relevant properties (e.g. weight, color, size, solidity, shape, etc…), and 2) Some number of behaviors that they can exhibit (e.g. being opened, making something else hot, etc…). These properties and behaviors are inseparable.
With traditional programming, the properties (data) and behaviors (functions) are separate entities, which means that traditional programming often does not provide a very intuitive representation of reality. It’s up to the programmer to manage and connect the properties to the behaviors in an appropriate manner.
Object-oriented programming (OOP) provides us with the ability to design an “object”, which ties together both properties and behaviors into a self-contained, reusable package. This allows programs to be written in a more modular fashion, which makes them easier to write and understand, and also provides a higher degree of code-reusability. Objects also provide a more intuitive way to work with our data by allowing us to define how we interact with the objects, and how they interact with other objects. Note that OOP doesn’t replace traditional programming methods. Rather, it gives you additional tools in your programming tool belt to manage complexity when needed.
Object-oriented programming also brings several other useful concepts to the table: inheritance, encapsulation, abstraction, and polymorphism (language designers have a philosophy: never use a small word where a big one will do). We will be covering all of these concepts in the upcoming tutorials over the next few chapters. It’s a lot of new material, but once you’ve been properly familiarized with OOP, you’ll likely never want to go back to pure traditional programming again.
|8.2 -- Classes and class members|
|7.x -- Chapter 7 comprehensive quiz|