Object-Oriented Programming
In this discussion, we will explore object oriented-programming with Java. Object-oriented programming (OOP) might sound intimidating, but don't let the name intimidate. You might find it easier to understand than other programming types because it models objects in the real world.
Let's start with a simple example.
Let's assume that a program is needed to keep track of an inventory of firewood. The real-world objects involved in this are pieces of wood and a woodpile. Using an object-oriented approach, we could model the woodpile as a real-world object. The woodpile has specific attributes that we are concerned with, including the amount and type of wood in stock. The woodpile should accept new loads of wood and track the quantity when pieces are used for a fire. The specifications for a woodpile is known as a class. The idea is to let the real-world attributes guide the creation of the classes used in the program.That is the basic idea behind Object-Oriented Programming. But what separates OOP from other types of programming? OOP programs are created by following four guiding principles of encapsulation, abstraction, inheritance, and polymorphism.
Encapsulation
When we model real-world objects in OOP, we mimic how the objects can change and act in the real world. In our woodpile example, let's assume that each woodpile object contains a count of wood pieces. In the computer, the count can be incremented simply by using addition. But in the real world, when the wood is added, sometimes more pallets and more room are needed to store the wood. If we let other programs freely add to the woodpile count, how would we know how many pallets are in use? How much room have we left? How would we know when we need another pallet? Instead of letting any program modify our wood count, OOP would have us lock the value down, hiding it from direct manipulation. A real-world behavior is added to the woodpile object that accepts new loads of wood. The behavior is known as a method, and the code is hidden from outside programs. Once that is done, the woodpile takes care of pallet and space allocation. The program delivering wood only needs to know how much wood it is delivering. That is known as encapsulation. It is beneficial because any change to the way we stack or store wood can be changed in the woodpile class, and the delivery programs don't need to change.
Abstraction
Object-oriented programming involves modeling real-world objects as classes. The best OOP programs keep their classes obedient to the real-world rules and properties they represent. This is abstraction; objects modeled from the real world should contain all of the information and interactions the objects do or have in the real world. For example, in the real world, you would not expect the person delivering your wood to give you a new total count of how much wood you have. Only you would know that. The delivery person only knows how much they are delivering. So in the real world, you would accept the wood and update the count. The use of abstraction would have our modeled woodpile know how to accept new wood and update its count.
Inheritance
Modeling real-world objects might sound like a daunting task. There are so many! Continuing our woodpile example, let's say that somebody asks to use our program in a windy area. They need to have weights on the top of the wood so that the covers stay on and the wood stays dry. Our wood class doesn't have this and live where it is not windy. Do we create an entirely new class modeled after our woodpile, duplicating all of the code and adding code to support the wind weighting? Inheritance is a better way around this. It allows us to create a child class covered woodpile, which is just like the woodpile but has extra properties or behaviors. The covered woodpile class would only know how to deal with the wind covers. It would use the behaviors of the parent class, woodpile, to add manage all other aspects of the woodpile. If a change is necessary to the way wood is added to the pile, it can be changed in one place, the parent class. That is inheritance in a nutshell.
Polymorphism
When modeling objects and behavior in OOP, an object sometimes needs to do a core behavior differently from its parent class. In the woodpile example, we created a child class covered woodpile, modeled after the parent class woodpile. An issue arises when we receive more wood because the covered woodpile would have us cover the wood and apply weights to the top every 2-3 feet. The method to receive wood is handled in the parent class. Here is where polymorphism ('many forms') comes in. Each child class that is created can override the behavior of the parent class. It means that the covered woodpile class would have its own method to receive wood. When wood is added, the delivery program only needs to know how much wood is being delivered. The covered woodpile class handles the override runs its code to add the wood. It can even use the parent's method to add the wood and then run its code to do the covering.
Installation
With these four principles in mind, let's get into some Java. Before writing any code, you will need two things: the Java runtime and a development environment. They can both be downloaded from the following links.
Example 1: Hello World!
Once installed, you are ready to write OOP using Java! Below is a sample 'Hello World' program to acquaint you with some syntactical information.
package helloworldapp;
public class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
The first line is the package declaration. A package is a group of one or more classes. Each class within a package must have a unique name. The second line declares the class HelloWorldApp. This class has only one method, and that method says hello. The third line declares the method to say hello, which is simply called main. The fourth line performs the program's behavior, which is to say, "Hello World!" Notice that the class methods and properties are surrounded by curly braces '{}.' These denote the beginning and end of a code block, respectively.
Summary
Now that you have a basic understanding of Object-Oriented Programming, be sure and go through the Java tutorials and documentation provided by Oracle. Before you know it, you will be on your way to Object-Oriented goodness!
No comments:
Post a Comment