Object-oriented programming (OOP) is a programming paradigm that is fundamentally different from traditional procedural programming styles. It is centered around the concept of objects-programming constructs that have both properties and the procedures for manipulating those properties. This approach models the real world much more closely than conventional programming methods and is ideal for the simulation-type problems commonly encountered in games.
You're probably already aware that Java is an object-oriented language, but you might not fully understand what that means. To successfully use Java to write Internet games, you need to embrace object-oriented programming techniques and design philosophies. The goal of today's lesson is to present the conceptual aspects of object-oriented programming as they relate to Java. By the end of today's lesson, you will fully understand what OOP means to Java and maybe even have some new buzz words to share with your friends! More important, you will gain some insight into why the OOP paradigm built into Java is a perfect match for game programming.
Post a Comment:
If you like this post please put a comment so that i can improve my site.
Thanks for visiting this site.
The following topics are covered in today's lesson:
· What is OOP?
· OOP and games
· Java and other OOP languages
What Is OOP?
If you've been anywhere near the computer section of a bookstore or picked up a programming magazine in the last five years, you've certainly seen the hype surrounding object-oriented programming. It's the most popular programming technology to come about in a long time, and it all revolves around the concept of an object. The advent of Java has only served to elevate the hype surrounding OOP. You might wonder what the big deal is with objects and object-oriented technology? Is it something you should be concerned with, and if so, why? Is it really that crucial when working with Java? If you sift through the hype surrounding the whole object-oriented issue, you'll find a very powerful technology that provides a lot of benefits to software design.
But the question still remains: What is OOP? OOP is an approach to programming that attempts to bridge the gap between problems in the real world and solutions in the computer programming world. Prior to OOP, a conceptual stumbling block always existed for programmers when trying to adapt the real world into the constraints imposed by a traditional programming language. In the real world, people tend to think in terms of "things," but in the pre-OOP programming world people have been taught to think in terms of blocks of code (procedures) and how they act on data. These two modes of thinking are very different from each other and pose a significant problem when it comes to designing complex systems that model the real world. Games happen to be very good examples of complex systems that often model the real world.
OOP presents an approach to programming that allows programmers to think in terms of objects, or things, much like people think of things in the real world. Using OOP techniques, a programmer can focus on the objects that naturally make up a system, rather than trying to rationalize the system into procedures and data. The OOP approach is a very natural and logical application of the way humans already think.
The benefits of OOP go beyond easing the pain of resolving real world problems in the computer domain. Another key issue in OOP is code reuse, when you specifically design objects and programs with the goal of reusing as much of the code as possible, whenever possible. Fortunately, it works out that the fundamental approaches to OOP design naturally encourage code reuse, meaning that it doesn't take much of an extra effort to reuse code after you employ standard OOP tactics.
The OOP design approach revolves around the following major concepts:
· Objects
· Classes
· Encapsulation
· Messages
· Inheritance
Objects
Objects are bundles of data and the code, or procedures, that act on that data.
The procedures in an object are also known as methods. The merger of data and methods provides a means of more accurately representing real-world objects. Modeling a real-world problem through traditional programming constructs, without objects, requires a significant logical leap. Objects, on the other hand, enable programmers to solve real-world problems in the software domain much more easily and logically.
As evident by the name, objects are at the heart of object-oriented technology. To understand how software objects are beneficial, think about the common characteristics of all real-world objects. Lions, cars, and calculators all share two common characteristics: state and behavior.
The state of an object is the condition that the object is in, as defined by its attributes.
The behavior of an object is the collection of actions that the object can take.
For example, the state of a lion might include color, weight, and whether the lion is tired or hungry. Lions also have certain behaviors such as roaring, sleeping, and hunting. The state of a car includes the current speed, the type of transmission, whether it is two- or four-wheel-drive, whether the lights are on, and the current gear, among other things. The behaviors for a car include turning, braking, and accelerating.
Just like real-world objects, software objects possess two common characteristics: state and behavior. To relate this back to programming terms, the state of an object is determined by its data and the behavior of an object is defined by its methods. By making this connection between real-world objects and software objects, you begin to see how objects help bridge the gap between the real world and the world of software living inside your computer.
Because software objects are modeled after real-world objects, you can more easily represent real-world objects in object-oriented programs. You could use the lion object to represent a real lion in an interactive software zoo. Similarly, car objects would be very useful in a racing game. However, you don't always have to think of software objects as modeling physical real-world objects; software objects can be just as useful for modeling abstract concepts. For example, the standard Java API provides a thread object that represents a stream of execution in a multithreaded program.
Classes
Throughout this discussion of object-oriented programming, you've only dealt with the concept of an object already existing in a system. You might be wondering how objects get into a system in the first place. This question brings you to the most fundamental structure in object-oriented programming: the class.
A class is a template or prototype that defines a type of object.
A class is to an object what a blueprint is to a house. Many houses can be built from a single blueprint; the blueprint outlines the makeup of the houses. Classes work exactly the same way, except that they outline the makeup of objects.
In the real world, there are often many objects of the same kind. Using the house analogy, there are many different houses around the world, but as houses they all share common characteristics. In object-oriented terms, you would say that your house is a specific instance of the class of objects known as houses.
An instance of a class is an object that has been created in memory using the class as a template. Instances are also sometimes referred to as instantiated objects.
All houses have states and behaviors in common that define them as houses. When a builder starts building a new development of houses, he or she typically will build them all from a set of blueprints. It wouldn't be as efficient to create a new blueprint for every single house, especially when there are so many similarities shared between each one. The same thing applies in object-oriented software development; why rewrite a lot of code when you can reuse code that solves similar problems?
In object-oriented programming, as in construction, it's also common to have many objects of the same kind that share similar characteristics. And like the blueprints for similar houses, you can create blueprints for objects that share certain characteristics. What it boils down to is that classes are software blueprints for objects.
As an example, the class for the car object discussed earlier would contain several variables representing the state of the car, along with implementations for the methods that enable the driver to control the car. The state variables of the car remain hidden underneath the interface. Each instance, or instantiated object, of the car class gets a fresh set of state variables. This brings you to another important point: When an instance of an object is created from a class, the variables declared by that class are allocated in memory. The variables are then modified via the object's methods. Instances of the same class share method implementations but have their own object data. Classes can also contain class data.
Object data, or instance data, is the information that models an object's state. Each object in memory has its own set of instance data, which determines what state the object is in.
Class data is data that is maintained on a class-wide basis, independent of any objects that have been created.
There is only one instance of class data in memory no matter how many objects are created from the class. Class data is typically used to store common information that needs to be shared among all instances of a class. A common example of class data is a count of how many instantiated objects exist of a particular class. When a new object is created, the count is incremented, and when an existing object is destroyed, the count is decremented.
Objects provide the benefits of modularity and information hiding, whereas classes provide the benefit of reusability. Just as the builder reuses the blueprint for a house, the software developer reuses the class for an object. Software programmers can use a class over and over again to create many objects. Each of these objects gets its own data but shares a single method implementation.
Encapsulation
Encapsulation is the process of packaging an object's data together with its methods.
A powerful benefit of encapsulation is the hiding of implementation details from other objects. This means that the internal portion of an object has more limited visibility than the external portion.
The external portion of an object is often referred to as the object's interface, because it acts as the object's interface to the rest of the program. Because other objects must communicate with the object only through its interface, the internal portion of the object is protected from outside tampering. And because an outside program has no access to the internal implementation of an object, the internal implementation can change at any time without affecting other parts of the program.
Encapsulation provides two primary benefits to programmers:
· Implementation hiding
· Modularity
Implementation hiding refers to the protection of the internal implementation of an object.
An object is composed of a public interface and a private section that can be a combination of internal data and methods. The internal data and methods are the sections of the object that can't be accessed from outside the object. The primary benefit is that these sections can change without affecting programs that use the object.
Modularity means that an object can be maintained independently of other objects.
Because the source code for the internal sections of an object is maintained separately from the interface, you are free to make modifications and feel confident that your object won't cause problems. This makes it easier to distribute objects throughout a system, which is a crucial point when it comes to Java and the Internet.
Messages
An object acting alone is rarely very useful; most objects require other objects to really do anything. For example, the car object is pretty useless by itself with no other interaction. Add a driver object, however, and things get more interesting! Knowing this, it's pretty clear that objects need some type of communication mechanism in order to interact with each other.
Software objects interact and communicate with each other via messages. When the driver object wants the car object to accelerate, it sends a message to the car object. If you want to think of messages more literally, think of two people as objects. If one person wants the other person to come closer, they send the other person a message. More accurately, one might say to the other person "come here, please." This is a message in a very literal sense. Software messages are a little different in form, but not in theory; they tell an object what to do. In Java, the act of sending an object a message is actually carried out by calling a method of the object. In other words, methods are the mechanism through which messages are sent to objects in the Java environment.
Many times, the receiving object needs more information along with a message so that it knows exactly what to do. When the driver tells the car to accelerate, the car must know by how much. This information is passed along with the message as message parameters.
From this discussion, you can see that messages consist of three things.
1. The object to receive the message (car)
2. The name of the action to perform (accelerate)
3. Any parameters the method requires (15 mph)
These three components are sufficient information to fully describe a message for an object. Any interaction with an object is handled by passing a message. This means that objects anywhere in a system can communicate with other objects solely through messages.
Just so you don't get confused, understand that "message passing" is another way of saying "method calling." When an object sends another object a message, it is really just calling a method of that object. The message parameters are actually the parameters to a method. In object-oriented programming, messages and methods are synonymous.
Because everything that an object can do is expressed through its methods (interface), message passing supports all possible interactions between objects. In fact, interfaces enable objects to send messages to and receive messages from each other even if they reside in different locations on a network. Objects in this scenario are referred to as distributed objects. Java is specifically designed to support distributed objects.
Inheritance
What happens if you want an object that is very similar to one you already have, but with a few extra characteristics? You just derive a new class based on the class of the similar object.
Inheritance is the process of creating a new class with the characteristics of an existing class, along with additional characteristics unique to the new class.
Inheritance provides a powerful and natural mechanism for organizing and structuring programs.
So far, the discussion of classes has been limited to the data and methods that make up a class. Based on this understanding, you build all classes from scratch by defining all of the data and all of the associated methods. Inheritance provides a means to create classes based on other classes. When a class is based on another class, it inherits all of the properties of that class, including the data and methods for the class. The class doing the inheriting is referred to as the subclass (child class) and the class providing the information to inherit is referred to as the superclass (parent class).
Note
Child classes are sometimes referred to as descendants, and parent classes are sometimes referred to as ancestors. The family tree analogy works quite well for describing inheritance.
Using the car example, gas-powered cars and cars powered by electricity can be child classes inherited from the car class. Both new car classes share common "car" characteristics, but they also have a few characteristics of their own. The gas car would have a fuel tank and a gas cap, and the electric car might have a battery and a plug for recharging. Each subclass inherits state information (in the form of variable declarations) from the superclass. Figure 3.3 shows the car parent class with the gas and electric car child classes.
Object-Oriented Programming with Java
/ On : 7:34 AM/ Thank you for visiting my small blog here. If you wanted to discuss or have the question around this article, please contact me e-mail at herdiansyah hamzah@yahoo.com.