|
|
Object Orientation in Java
Object Orientation in
Java
- 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 create
an an instance of the class or to instatiate an object. Thus, the
blueprint is used to actually create an object which is
stored in the computer's memory. The object will actually
be able to utilize the methods and fields defined by its
class.
| Many people use the following metaphors
to describe the relationship between a class and an object:
You can think of a class as a cookie cutter and an instance as an
actual cookie. Similarly, you can think of a class as a
blueprint 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 class
during the execution of a program. What's more, each of these
instances has its own copy of methods and fields so that over
the life of the program each can grow independently.
- Thus,
for example, a Button class might be used to instantiate two
buttons in a program. Once instantiated, each Button object
has its own label field and background color field. During
the program, one button might be set to be Blue and to say
"Hello Cyberspace". Another might be set to red and "Hello
Selena." Both remain Button objects, but they gain unique
features.
| You can compare two objects to see if they
are built from the same class by using the "instanceof" keyword
such as in the following example:
if (someObject instanceOf Button)
{
...do something....
}
|
Additional Resources:
Flow Control
Table of Contents
Methods
|
|