Java Reference
In-Depth Information
Two architects asked to design a house will certainly come up with two
unique but satisfactory solutions. Two developers asked to solve a problem
using an OOP language will certainly come up with two unique (and hope-
fully satisfactory) solutions. One solution may be more elegant than the other.
One solution might be easier to maintain or change than the other.
Entire topics are written on how a problem can be solved using OOP. Object-
oriented analysis and design (OOAD) refers to this process of designing pro-
grams that will be solved using an OOP language.
We do not have time to delve into the various OOAD approaches and give
them the justice they deserve. My goal for you at this point in your OOP learn-
ing is to have you understand the big picture about OOP so that you can start
developing and understanding Java programs.
At its core, OOP is based on writing classes for the objects in the problem
domain: that is, the objects in the problem being solved. An object is any noun
that appears in your problem or that can be used to help solve the problem.
A class is written for each object in the problem domain. These classes are
then instantiated in your program, thereby creating the objects in memory for
use by your program.
Writing a Java Class
A class in Java is declared using the class keyword. A source code file in Java
can contain exactly one public class, and the name of the file must match the
name of the public class with a .java extension.
You can declare more than one class in a .java file, but at most one of the
classes can be declared public . The name of the source code file must still
match the name of the public class. If there are no public classes in the
source code file, the name of the file is arbitrary.
The fields and methods of a class appear within the curly brackets of the
class declaration. The following code shows a simple class with no fields or
methods declared yet.
public class Employee
{
}
Adding Fields to a Class
The attributes of an object become fields in the corresponding class. A field
within a class consists of the following:
Search WWH ::




Custom Search