- Okay, so now that we understand the most basic Java syntax it is time to learn how object-oriented design is implemented in Java.
Java Classes - The basic unit of object-orientation in Java is the class. The class is often described as a blueprint for an object. It allows a programmer to define all of the properties and methods that internally define an object, all of the API methods that externally define an object, and all of the syntax necessary for handling encapsulation, inheritance and polymorphism.
- When a virtual machine needs to create an actual object from the class' blueprint, it is said to createan "instance" (or instantiate) of the class. Thus, theblueprint is used to actualy create an object which isstored in the computer's memory. The object will actuallybe able to utilize the methods and fields defined by its class.
| Many people use the following metaphorsto describe the relationship between a class and an object:You can think of a class as a cookie cutter and an instance as anactual cookie. Similarly, you can think of a class as ablueprint of a house and an instance as an actual house. |
- It is also important to know that more than one instance can be fabricated from the same classduring the execution of a program. What's more, each of theseinstances has its own copy of methods and fields so that overthe life of the program each can grow independently.
- Thus,for example, a Buton class might be used to instantiate twobuttons in a program. Once instantiated, each Button has its own "Label" field and its own Background Color field. Duringthe program, one button might be set to be Blue and to say"Hello Cyberspace". Another might be set to red and "HelloSelena." Both remain Button objects, but they gain unique features.
| You can compare two objects to see if theyare built from the same class by using the "instanceof" keywordsucgh as in the following example: if (someObject instanceOf Button) { ...do something.... } |
Previous Page |Next Page
|