Object Oriented Programming Overview
If you are not familiar with an object-oriented programming language, you will first need to understand the foundation that makes up this paradigm. It is a necessity for anyone who plans on writing code. In this article, I will explain the basic OOP structures in detail.
The first thing that you will want to become familiar with is an object. An object is a bundle of software that contains methods and variables. The objects which are found in computer programs will often be used to simulate objects which exist in the real world. To truly understand OOP, it is crucial that you understand the importance of objects. Before I discuss objects in the OPP sense, look around you. It is likely that you see a number of objects, and this could include your desk, computer, or chair.
There are two things that all objects are composed of, and these are "behavior" and "state." As an example, cats have a state, and these states are color, breed, or name. They will also have behavior and one example of this could be purring. Even non-living objects have a state. The state of a bike could be the number of gears or the current gear. The behavior of a bike could be changing gears or braking. As you can see, both living and non-living objects will have both behavior and state. These objects include software programs. Objects that exist within software are based on objects which exist in reality. They will have behaviors and states.
An object that exists in software may hold its state in more than one variable. A variable can be defined as information that has been identified and given a name. The object will use its methods and behaviors. A method is an action that is carried out by the object. It may also be called a function, and is always connected with the object. So, as you can see, an object is a piece of software that is composed of both methods and variables. Objects that exist in software can be used to represent objects which exist in the real world. For example, if you are creating a motorcycle within a computer animation program, you will want it to behave like its real world counterpart.
Not only can you use objects to model things which exist in reality, but you can also use them to represent things which are more abstract. An "event" is a standard object that is used to demonstrate the motion of a user pressing on their mouse or keyboard. Everything that exists within the object is aware of its state, and can perform behaviors based on the methods and variables. For example, an object that would model a real world motorcycle would have variables which are related to the state of the vehicle. Some of these variables include speed, current gear, and pedal cadence. In object oriented programming terminology, this is called an instance variable. The reason for this is because it has information related to the state of the motorcycle.
Not only must the motorcycle have variables, but it will also need to have methods. An example of methods would be brakes or engine changes. The speed of the motorcycle is not a methods because it is merely a result of the engine changes. The name for these specific methods are called instance methods. The reason for this is because they will alter the state of motorcycle instance. The variables will comprise the center of an object. Methods will surround the center and protect it from the other objects that may exist in the program. Protecting the variables within the center of an object by surrounding it with methods is called encapsulation.
This is the image that object oriented programmers will seek to create. Sometimes the object may reveal a few of its variables or hide a few of the methods.
Some languages such as Java will create access levels for the variables or methods. The access level will only allow certain classes or objects to access it. However, there are a couple of powerful advantages to encapsulating variables or methods. The first is information-hiding, and the second is modularity. Modularity allows the code within an object to exist without being attached to other objects, and information-hiding can allow the object to hide information that is not effected by other objects.