Anda di halaman 1dari 4

What is Object-Oriented Programming ? OO programming is designed so that real world concepts can be modeled in a computer program.

This concept is rather difficult to explain so I'll use an example throughout this primer. The example I am going to use is that of a bicycle. Object-oriented programming thinks of real world things as objects, so in the case of the example the bicycle is an object. Objects have two parts to them, data and operations which can be carried out on this data. So in the bicycle example, the data might contain the speed of the bike, the height of the seat and whether the bell is currently being rung or not. There are several different operations which can be carried out on the bicycle, the rider may pedal faster, they might want to change the height of the seat of ring the bell. So in this simple object we have the following: Data: Speed Height of seat Status of the bell Operations: Pedal Faster Adjust Height of Seat Ring Bell Encapsulation Encapsulation is an important part of OO programming, but it's not difficult. In Java Encapsulation is implemented by a class, all a class is the generic form of an object. So in our example the class is bicycle while the object (an instance of the class) is, perhaps MyBicycle. Within the object there are so called instance variables, in our example MyBicycle will have instance variables for speed, height of seat and status of the bell. So when an operation (in Java a method) operates on an object it changes the instance variables for that object. If the Pedal Faster method was called for the object MyBicycle then the instance variable of speed, within MyBicycle, would be altered accordingly. An additional complexity is that methods and instance variables can be public or private. If a method is private it can only be accessed by code within the implementation of the class, while private instance variables can only be accessed from within the class. Public methods and instance variables can be accessed from any class.

Inheritance Inhertiance is a relatively simple concept which allows one class to extend another, or to inherit characteristics. When a class is extended a hierarchy is built up with the original class at the top and the classes which build on the class above it (the class at the top is known as the super-class) below. When a class is extended, to create a sub-class, all of the properties (variables and methods) of the original class still exist within the new class along with others which have been added. The bicycle in the example is a very simple one, if we wanted to extend it to have gears we would simply create a new class based on Bicycle but which had a new variable called gear and two new methods, one to change up a gear and one to change down. Polymorphism Polymorphism is the ability of objects to react differently when presented with different information, known as parameters. In a functional programming language the only way to complete two different tasks is to have two functions with different names. Object-oriented languages, which include Java, allow different methods to be run depending on what type of parameters are specified. So if our bicycle were to be involved in a collision with a object the object would be passed as a parameter to a collision method, obviously collision with a fly will have very different affects to collision with a lorry. The fly may cause the bicycle to gain a speck of blood while collision with a lorry may cause a sudden loss of speed and major damage. This ability of a method to react to different parameters is achieved by overriding, a number of methods are written (with the same name) but each one has a different set of input parameters. So for our example there will be two methods called collision, one which accepts a parameter of type fly while the other accepts a parameter of type lorry. Conclusion on Object-Oriented Programming Object-oriented programming is fundamentally different to traditional functional programming, used correctly it can lead to the development of very robust, easily expandable and maintainable code. Objects are everywhere around us, while the class they belong to is not always obvious. We use encapsulation all the time, when we change gear in a car we do not need to know what happens with the clutch or gearbox we simply need to know that moving the gear lever will change gear. If this were likened to an object, the gear lever would be the method while the internal workings of the clutch and gearbox would be the instance variables. Expanding this simple car to be a racing car with

wings and spoilers is an example of inheritance while the car's ability to cope with steering movements to the left and right is an example of polymorphism. Info about Abstraction: Abstraction is a design technique that focuses on the essential aspects of an entity and ignores or conceals less important or non-essential aspects. Abstraction is an important tool for simplifying a complex situation to a level where analysis, experimentation, or understanding can take place. For example, in attempting to understand the mechanics of the solar system, early mathematicians and astronomers applied abstraction to a "planet", treating the planet as a body all of whose mass is concentrated at a single point. Thus, abstraction ignores a wealth of details about each planet - its actual diameter, its atmospheric content, its average temperature, etc. However, these other details are not relevant to understanding and modeling the basic orbital mechanics of the solar system. Abstraction is concerned with both the "attributes" and "behavior". Attributes refer to the properties or characteristics associated with an entity. Attributes typically correspond to the data that is recorded for an object. For a sales tracking system relevant attributes of a salesperson might be: name, number of vehicles sold, value of vehicles sold, list of customers, commission rate, total commissions. The "behavior" represents the set of actions that the object can perform. An action usually corresponds to an action that the real-world entity might perform. Actions for a "salesperson" might include "sellCar", "reportIncome" and "increaseCommisionRate". Abstraction is vital to creating tractable software objects because the real-world objects are far too complex to be captured in complete detail. Consider the simple "salesperson" object referred to above. A real "salesperson" has an identity, a family genealogy, a medical history, a genetic profile, a credit record, a set of talents, and many more. Similarly there is a rich set of actions of which the salesperson is capable (singSong, doDance, haveChild, getSick, increaseCreditLimit, payBills, etc.). Trying to capture even a small part of this enormous detail in a software object is pointless. What is important is to capture those aspects of a "salesperson" that are relevant to the development of the sales tracking system. The objects in an object-oriented system are often intended to correspond directly to entities in the "real world". Objects such "salesperson" and "automobiles" that might occur in an automobile dealership tracking system correspond to the actual people on the staff of the dealership and the actual cars owned and sold by the dealership. The correspondence between the software objects and the real-world entity that they represent is often so direct and real that

computer-based theft or fraud often involves tampering with the software objects that are trusted by others to correspond to real-world artifacts. This sense of correspondence is also expressed as the "program" being a "simulation" or " model" of the real-world, changes in one being reflected in the other. A "good" program is one which models or simulates accurately what is happening in the real-world.

Anda mungkin juga menyukai