Java Reference
In-Depth Information
You can list all properties and behaviors for you and your friend separately and examine them
separately as if there is no connection between you and your friend.
You can list the properties and behaviors for you and your friend that are in common and
then examine them as properties and behavior for an entity without naming you and your
friend. This model assumes that all listed properties and behaviors will be present in an
entity (without naming it) though they may vary from entity to entity. You may want to list
all properties and behaviors for you and your friend as properties and behavior of a class,
say human, and treat you and your friend as two different instances of that human class.
Essentially, you have grouped together entities (e.g., you and your friend) with similar
properties and behaviors, and called that group a class. Then you will treat all objects (again,
you and your friend) as instances of that class.
The first approach treats each object as a separate entity. In the second approach, objects are classified based
on similarity of properties and behaviors where an object always belongs to a class; the class becomes the essential
part of programming. To determine any property or behavior of an object, you need to look up its class definition.
For example, you are an object of the human class. Can you fly? This question can be answered by going through a
series of steps. First, you need to answer the question “What class do you belong to?” The answer is that you belong
to the human class. Does the human class define a flying behavior? The answer is no. Because you are an instance of
the human class that does not define the flying behavior, you cannot fly. If you look carefully at the way you arrived
at the answer, you would find that the question is asked on an object (you), but the answer was provided by the class
(human) to which the object belongs.
Classes are essential, and they are basic parts of programs in object-oriented programming. They are used as
templates to create objects. Let's discuss how to define a class. A class in Java may consist of five components:
Fields
Methods
Constructors
Static initializers
Instance initializers
Fields and methods are also known as members of the class. Classes and interfaces can also be members of
a class. This chapter focuses only on fields and methods. I will discuss classes and interfaces as class members in
Chapter 2 of Beginning Java Language Features (ISBN: 978-1-4302-6658-7). A class can have zero or more
class members.
Constructors are used to create objects of a class. You must have at least one constructor for a class.
Initializers are used to initialize fields of a class. You can have zero or more initializers of static or instance types.
The rest of this chapter will discuss how to declare and use the different components of a class.
Declaring a Class
The general syntax for declaring a class in Java is
<<modifiers>> class <<class name>> {
// Body of the class goes here
}
 
Search WWH ::




Custom Search