articles

Home / DeveloperSection / Articles / Understanding a Java Object

Understanding a Java Object

Ailsa Singh1412 10-May-2016

Java is known as an object-oriented programming language (OOPS). The designers created it in a way such that objects become the center of attention. Java programs create and manipulate objects in an attempt to model how the real world operates.

When we design a class, think about the objects that will be created from that
class type. Think about:

·         things the object knows

·         things the object does

For our purposes, an object is an entity that has:

·    A state and (What objects knows)

·    Methods to manipulate that state (What object does)

 The state of an object is determined by its attributes.

Take a simple example, we can think of a person as an object. A person has several attributes like name, age, height, gender, color of hair, body colour, and so on. Within a program, every attribute can be represented by an appropriate variable; for example,

·         a String variable can  be represented as name

·         an int variable can  be represented as age

·         a char variable can  be represented as gender

·         a double variable can  be represented as height, and so on.

We generally use the term field names (or, more simply, fields) to refer to these variables. Therefore, the state of an object is defined by the values in its fields. In addition, we will require methods to  get, set and/or change the values of the fields as well as to retrieve their values. For instance, if we want to know  a person’s height, we would need a method to “look into” the object and return us the value of the height field.

A car is another good example of an object. It has attributes such as make, model, number of seats, fuel capacity, and actual fuel in the tank, mileage, type of stereo system, and speed. Similarly, A book object consists of attributes such as author, title of the book, price, number of pages, type of binding (hardcover, paperback, spiral), and if it is in stock or not. A person, a car, and a book are common examples of concrete objects. Note, however, that we can also represent objects for abstract concept such as a department in a software development company like MindStick or a faculty in a university.

In the previous example, we did not speak of a particular person. Rather, we spoke of a general category “person” such that each one in this category has the attributes mentioned. (Similar notion apply to car and book.) In Java terminology, “person” is a class. We think of a class as a general category (a template or a blue print) from which we can create individual objects.

An object, then, is said to be an instance of a class; in this example, a Person object would refer to a specific person. To work with two Person objects, we would require to create two objects from the class definition of Person. Every object would have its own copy of the field variables (also called instance variables); the values of the variables in one object could be different from the values of the variables in the other object.


Updated 16-Dec-2017

Leave Comment

Comments

Liked By