On the very first day,i mentioned about OOP i.e. Object Oriented Programming.Let us elaborate this term and understand what are the objects around which JAVA is oriented.
Objects are the components used to build OOP computer programs; each object represents a part of the system. Looking at your Web browser, you saw that objects can represent visible things such as buttons and scrollbars. But OOP is useful for more than merely creating user interfaces.
In an OOP program, everything of importance to the program is represented by an object. Objects can represent real things, like employees or automobile parts or apartment buildings; but objects can also represent more abstract concepts, such as relationships, times, numbers, or even black holes and transfinite dimensions.
Regardless of what kind of objects you use, you’ll want to be able to recognize an object when you meet one. That’s easy to do, because all objects have three properties:
- Identity: who the object is
- State: the characteristics of the object
- Behavior: what the object can do
Let’s take some time to explore these concepts.
Object Identity
Inside your computer program, every object has its own identity . Probably the easiest way to think about identity is to think of it as the object’s name. In this respect, objects act a little bit like variables .
Variables are names we give to data elements that can vary (hence the name), or change as a program runs. You can create a pair of numeric variables in Java like this:
int littleInt, bigInt;
The variables littleInt and bigInt now represent different areas of memory where you can store integer values. If you put a value into the integer named littleInt, it won’t affect the variable bigInt at all. The two variables have different identities.
Objects are a little more complex than simple numeric variables. The name of an object does not represent a particular area of memory, as the name of a simple variable does. Instead, the name of an object refers to, or points to, a particular area of memory. Thus, the same object can have several different names.
Of course, in real life, you cope with this all the time; your children call you “Dad,” your wife calls you “Honey,” and your boss calls you by your given name, all without any confusion as to your real identity. In a similar manner, a single object can have many different names.
Object State
The second property shared by every object is state. The state of an object includes all the information about the object; that is, its attributes or characteristics. In an object-oriented program, each object stores its state in fields or instance variables.
The easiest way to understand state is to look at an example. Take a look at the “Back” button on your Web browser–the button used to return to the last Web page that you’ve visited.
![The back button on a Web browser](https://i0.wp.com/sofia.fhda.edu/gallery/java/unit02/back_btn.gif)
You’ve already seen that a button is an object; let’s see if you can identify some of the fields that a backButton object (just to give it a name), needs in order to do its job.
If you take a piece of paper and start describing the button, you’ll find you need attributes such as the following:
- Position: where the button is located on the screen
- Size: the button’s width and height
- Caption: any text, such as the word “Back,” that the button displays
- Image: any icon or image that is displayed on the button’s surface
- Clicked: whether or not the button is currently selected (pressed)
Each of these attributes can be stored in a field in the backButton object. The state of the object is represented by the combination of all of its fields. For instance, the backButtonobject may display an arrow image but no text, and, if you click on the button with your mouse, it may be depressed. When you stop clicking, the state of the object will change because the value stored in the Clicked field will change from true to false.
The attributes of an object thus remain unchanged, but the state of your object changes as the values stored in its attributes change. Your backButton will always have a Clickedattribute, but the value stored in the Clicked field–and your object’s state–can vary as your program runs.
Object Behavior
The third property shared by all objects is behavior. In Java, the behaviors of an object are represented by procedures called methods.
If you want a particular object to perform some action, you invoke one of its methods. To accent the fact that objects represent fairly self-contained, autonomous units, the process of invoking a method is called sending a message to the object. If you were writing a Web browser program and you wanted to interact with the backButton object that you previously met, you could ask it to change its size, for instance, by sending it a message with the desired size like this:
backButton.setSize(300, 100); |
If the backButton object had a method called setSize() , it would obediently carry out your wishes. Unlike an object’s fields, which are generally hidden from outside view, most methods are public , readily accessible to other objects.
- Before moving ahead,i want to raise one point that JAVA is a purely an OOP language unlike the earlier studied languages like C , C++.
The next very important concept is CLASSES in JAVA.
If object-oriented programs are collections of cooperating objects, then just what are
classes? Although often confused, the difference between classes and objects is simple:
A class represents the definition–the blueprint if you like–that is used to construct an object.
Every object is the run-time incarnation or instantiation of a particular class. A class defines the characteristics of each individual object, and each class can be used to produce many objects. The class shown here, for instance is used to define the characteristics of a laptop computer. The same formula or blueprint can be used to produce any number of individual computers.
In object-oriented programs, classes represent a concept that is similar to the concept of variable types. Recall the two numeric variables, bigInt and littleInt, from earlier in this lesson? Both variables share a common type: int. Because they are ints, they can store a certain range of values. The values -12, 0, and 23 are valid ints, but the value 1.5 is not. Also, because bigInt and littleInt are integers, they have certain inherent capabilities; you can divide bigInt by littleInt, for instance.The class of an object describes its attributes (the kinds of states it can assume) and its behaviors (the kinds of operations it can perform). If you return to your Web browser again, and this time turn your attention to the “Forward” button, (let’s call it forwardButton), you’ll immediately notice that is the same “kind” of object as the backButton object you looked at previously, even though it contains a different image and a different caption. The two buttons share a common class.
As you begin writing Java programs, you’ll find that classes are very important. In fact, every Java program you write is a class definition . In your class definitions you describe the attributes that each of your objects possesses, and define the methods that each uses to carry out its actions.
As you write your class definitions, you’ll rely on three very important OOP principles: encapsulation, inheritance , and polymorphism.