0% found this document useful (0 votes)
31 views4 pages

Understanding Java Class Structure

Uploaded by

contato.rcs7
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views4 pages

Understanding Java Class Structure

Uploaded by

contato.rcs7
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CHAPTER 4

The “Outer Language”


of Java

T HE EARLIER SECTIONS OF THIS book focussed on what can be done


within a single “top level” class. A typical Java program consists of
multiple top-level classes, and may also use abstract classes, interfaces,
any or all of the four types of inner class, as well as the specialized Enum
class. While it is not standard terminology, you can think of this
structure as the “outer language” of Java.

4.1 CLASS STRUCTURE


A class defines a type of object. It tells what data objects of that type
contain and what methods they have. Here are the most important parts
of a class definition:
ESTRUTURA DE
UMA CLASSE,
access class ClassName { COM SPRING
fields PODEM HAVER
constructors ANOTAÇÕES EM
methods CIMA.
}

Every object has a type, and the name of that type is the same as the
ClassName. If you define a class Item, then every object created from
that class will be of type Item.

DOI: 10.1201/9781003402947-4 69

More books: [Link]


70 ▪ Quick Java

Normally, every top-level class goes in a separate file, and the name of the
file must be the name of the class, with the [Link].

Note: More than one top-level class can be put in a file, but only one
of them can be public, and that is the one the file must be named
after. However, one class per file is the recommended organization.

4.1.1 A Simple Class


We will begin by looking at a simple example of a class; later sections will
describe each of the parts of a class in detail.

The name of the class is Password, and each object created from the class
will hold one password. The method matches will tell us if a password is
correct, and the reset method will allow us to change it. Without fur­
ther ado, here’s the class:

public class Password {


private String password;
public Password(String password) {
[Link] = password;
}
public boolean matches(String attempt) {
return [Link](password);
}
boolean reset(String oldPassword,
String newPassword) {
if (matches(oldPassword)) {
password = newPassword;
}
return [Link](newPassword);
}
}

The name of the class is Password, and it is public, so it can be seen and
used anywhere in the project.

The class has a field named password, of type String. It is private, so it


cannot be seen or used outside this class.

More books: [Link]


The “Outer Language” of Java ▪ 71

Next in the class is a constructor. It has the same name, Password, as the
class that it is in, and it will return a Password object. (Constructors do
not use an explicit return statement.)

Notice that the constructor has access to two different variables, both
named password. One is the field, or instance variable, while the other is
a parameter. This may look strange, but it is quite commonly done,
especially in constructors. To distinguish between them, the name
password by itself refers to the parameter, while [Link] refers
to the instance variable.

Next is a method named matches, which will return a boolean (true or


false) value. Because strings are objects, the matches method must
compare them using equals.

Note: Although using == to compare strings often works, it won’t


work here; see section [Link] for an explanation. You should
always use equals to compare strings.

Finally, there is a reset method that takes two strings as parameters and
tells whether the reset succeeded. It is not marked public, protected,
or private, so it has package access—it can be used by any class in the
same package.

4.1.2 The Class Header


The class header may be as simple as the word class followed by the
name of the class, or it may be as complex as the following:

access class ClassName extends SuperClass


implements Interface1, … , InterfaceN

The access may be either public or omitted:

• public: Can be seen and used anywhere in the project.


• package: (default, no keyword) Can be seen and used by any classes
in the same package (directory). The package keyword, which
specifies which directory this class is in, cannot be used here.

More books: [Link]


72 ▪ Quick Java

There are two additional access types:

• protected: Can be seen and used by any classes in the same


directory, and by any subclasses of this class, wherever they may be.
• private: Can be seen and used only within this one class and, if in
an inner class, by its enclosing classes.

Fields and methods can be labeled with any one of the above access
types, with “package” as the default. Local variables of a method cannot
be so labeled; they’re just local.

By convention, the ClassName should begin with a capital letter. It defines


a type; every object created from this class will be of type ClassName.

Classes are arranged in a hierarchy, with the Object class at the root
(top) of the hierarchy. Every class except Object extends one other class,
called its superclass, and inherits data and methods from that superclass.
If the superclass is omitted, Object is assumed.

Briefly, inheritance means that if class B extends class A, then all the data
and methods in A are also part of B. B can extend what it receives from A
by defining additional data and methods. This will be examined in detail
in section 4.2.

The class may also implement some interfaces, as described briefly in the
next section.

4.1.3 Interfaces I
An interface is a list of methods—just the method headers, not the body. A
class that implements an interface must supply those methods, with the
same headers but also with a body. For example, the String class imple­
ments the CharSequence interface, and that interface requires the String
class to supply several methods, among them charAt(index), length(),
and isEmpty().

A class may extend another class, thus inheriting all its fields and methods.
Similarly, an interface may extend another interface, thus inheriting
additional methods to be implemented.

More books: [Link]

You might also like