Saturday, November 23, 2019

An Object in Java Represents a Real-World Object

An Object in Java Represents a Real-World Object An object in Java  - and any other object-oriented language  - is the basic building block of all Java applications and represents any real-world object you might find around you: an apple, a cat, a car or a human. The two characteristics that an object always has are state and behavior. Consider a person object. Its state might include hair color, sex, height, and weight, but also feelings of anger, frustration or love. Its behavior could include walking, sleeping, cooking, working, or anything else that a person might do. Objects form the very core of any object-oriented programming language. What is Object Oriented Programming? Hundreds of books have been written to describe the intricacies of object-oriented programming, but basically, OOP is based on a holistic approach emphasizing  re-use and inheritance, which streamlines development time.  More traditional procedural languages, such as Fortran, COBOL, and C, take a top-down approach, breaking down the task or problem into a logical, orderly series of functions. For example, consider a simple ATM application used by a bank. Before writing any code, a Java developer first will create a roadmap or plan on how to proceed, usually beginning with a list of all the objects that need to be created and how they will interact. Developers may use a class diagram to clarify the relationships between objects. Objects required for use in an ATM transaction might be Money, Card, Balance, Receipt, Withdrawal, Deposit and so on.  These objects need to work together to complete the transaction: making a deposit should result in a balance report and perhaps a receipt, for instance. Objects will pass messages between them in order to get things done. Objects and Classes An object is an instance of a class: here is the crux of object-oriented programming and the idea of re-use. Before an object can exist, a class on which it can be based must exist.   Perhaps we want a book object: to be precise, we want the book The Hitchhikers Guide to the Galaxy. We first need to create a class Book. This class could be the basis for any book in the world. It might look something like this: public class Book {String title;String author;   //methodspublic String getTitle({return title;}public void setTitle(){return title;}public int getAuthor(){return author;}   Ã‚  public int setAuthor(){return author;}// etc.} The class Book has a title and an author with methods that allow you to set or get either of these items (it would have more elements as well, but this example is just an excerpt). But this is not yet an object  - a Java application cant yet do anything with it.  It needs to be instantiated to become an object that can be used.   Creating an Object The relationship between an object and a class  is such that many objects can be created using one class. Each object has its own data but its underlying structure (i.e., the type of data it stores and  its behaviors) are defined by the class. We can create several objects from a book class. Each object is called an instance of the class. Book HitchHiker new Book(The HitchHikers Guide to the Galaxy, Douglas Adams);Book ShortHistory new Book(A Short History of Nearly Everything, Bill Bryson);Book IceStation new Book(Ice Station Zebra, Alistair MacLean); These three objects can now be used: they can be read, purchased, borrowed or shared.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.