{"id":383,"date":"2007-10-21T00:00:00","date_gmt":"2007-10-21T00:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=383"},"modified":"2007-10-21T00:00:00","modified_gmt":"2007-10-21T00:00:00","slug":"What Is an Object?","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=383","title":{"rendered":"What Is an Object?"},"content":{"rendered":"<p><DIV id=PageTitle>What Is an Object?<\/DIV><br \/>\n<BLOCKQUOTE>Objects are key to understanding <I>object-oriented<\/I> technology. Look around right now and you&#8217;ll find many examples of real-world objects: your dog, your desk, your television set, your bicycle.<br \/>\n<P>Real-world objects share two characteristics: They all have <I>state<\/I> and <I>behavior<\/I>. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming. <\/P><br \/>\n<P>Take a minute right now to observe the real-world objects that are in your immediate area. For each object that you see, ask yourself two questions: &#8220;What possible states can this object be in?&#8221; and &#8220;What possible behavior can this object perform?&#8221;. Make sure to write down your observations. As you do, you&#8217;ll notice that real-world objects vary in complexity; your desktop lamp may have only two possible states (on and off) and two possible behaviors (turn on, turn off), but your desktop radio might have additional states (on, off, current volume, current station) and behavior (turn on, turn off, increase volume, decrease volume, seek, scan, and tune). You may also notice that some objects, in turn, will also contain other objects. These real-world observations all translate into the world of object-oriented programming. <\/P><br \/>\n<P><\/P><br \/>\n<CENTER><br \/>\n<CENTER><br \/>\n<P><IMG height=210 alt=\"A circle with an inner circle filled with items, surrounded by gray wedges representing methods that allow access to the inner circle.\" src=\"http:\/\/java.sun.com\/docs\/books\/tutorial\/figures\/java\/concepts-object.gif\" width=345 align=bottom><\/P><br \/>\n<P class=FigureCaption>A software object.<\/P><\/CENTER><\/CENTER><br \/>\n<P>Software objects are conceptually similar to real-world objects: they too consist of state and related behavior. An object stores its state in <I>fields<\/I> (variables in some programming languages) and exposes its behavior through <I>methods<\/I> (functions in some programming languages). Methods operate on an object&#8217;s internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object&#8217;s methods is known as <I>data encapsulation<\/I> \u2014 a fundamental principle of object-oriented programming. <\/P><br \/>\n<P>Consider a bicycle, for example: <\/P><br \/>\n<P><\/P><br \/>\n<CENTER><br \/>\n<CENTER><br \/>\n<P><IMG height=218 alt=\"A picture of an object, with bibycle methods and instance variables.\" src=\"http:\/\/java.sun.com\/docs\/books\/tutorial\/figures\/java\/concepts-bicycleObject.gif\" width=273 align=bottom><\/P><br \/>\n<P class=FigureCaption>A bicycle modeled as a software object.<\/P><\/CENTER><\/CENTER>By attributing state (current speed, current pedal cadence, and current gear) and providing methods for changing that state, the object remains in control of how the outside world is allowed to use it. For example, if the bicycle only has 6 gears, a method to change gears could reject any value that is less than 1 or greater than 6.<br \/>\n<P>Bundling code into individual software objects provides a number of benefits, including: <\/P><br \/>\n<OL><br \/>\n<LI>Modularity: The source code for an object can be written and maintained independently of the source code for other objects. Once created, an object can be easily passed around inside the system.<br \/>\n<P><\/P><\/LI><br \/>\n<LI>Information-hiding: By interacting only with an object&#8217;s methods, the details of its internal implementation remain hidden from the outside world.<br \/>\n<P><\/P><\/LI><br \/>\n<LI>Code re-use: If an object already exists (perhaps written by another software developer), you can use that object in your program. This allows specialists to implement\/test\/debug complex, task-specific objects, which you can then trust to run in your own code.<br \/>\n<P><\/P><\/LI><br \/>\n<LI>Pluggability and debugging ease: If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace <I>it<\/I>, not the entire machine. <\/LI><\/OL><\/BLOCKQUOTE><br \/>\n<DIV><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What Is an Object? Objects are key to understanding object-oriented technology. Look around right now and you&#8217;ll find many examples of real-world objects: your dog, your desk, your television set, your bicycle. Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging &hellip; <a href=\"https:\/\/www.strongd.net\/?p=383\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">What Is an Object?<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-383","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/383","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=383"}],"version-history":[{"count":0,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/383\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}