Java Reference
In-Depth Information
•
Encapsulation
means that the data and the actions are combined into a single item
(in our case, a class object) and that the
details of the implementation are hidden.
Making all instance variables private is part of the encapsulation process.
•
A class can have two (or more) different definitions for the same method name,
provided the two definitions have different numbers of parameters or some
parameters of differing types. This is called
overloading
the method name.
•
A constructor is a variety of method that is called when you create an object of the
class using
new
. A constructor is intended to be used to perform initialization tasks
such as initializing instance variables. A constructor must have the same name as
the class to which it belongs.
•
A constructor with no parameters is called a
no-argument constructor
. If your class
definition includes no constructor definitions at all, then Java automatically pro-
vides a no-argument constructor. If your class definition contains any constructor
definitions at all, then no additional
constructors
are provided by Java. Your class
definitions should usually include a no-argument constructor.
Answers to Self-Test Exercises
1.
public void
makeItNewYears()
{
month = "January";
day = 1;
}
2.
public void
yellIfNewYear()
{
if
( (month.equalsIgnorewCase("January")) && (day == 1) )
System.out.println("Hurrah!");
else
System.out.println("Not New Year's Day.");
}
3.
public int
getNextYear()
{
int
nextYear = year + 1;
return
nextYear;
}
4.
public void
happyGreeting()
{
int
count;
for
(count = 1; count <= day; count++)
System.out.println("Happy Days!");
}