CertPrs8/Java 5 Cert. StudyGuide/Sierra-Bates/225360-6/Chapter 1
Java Refresher
3
though, Java programmers (and Sun) have created conventions for naming methods,
variables, and classes.
Like all programming languages, Java has a set of built-in keywords. These
keywords must not be used as identifiers. Later in this chapter we'll review the details
of these naming rules, conventions, and the Java keywords.
Inheritance
Central to Java and other object-oriented languages is the concept of inheritance,
which allows code defined in one class to be reused in other classes. In Java, you
can define a general (more abstract) superclass, and then extend it with more
specific subclasses. The superclass knows nothing of the classes that inherit from it,
but all of the subclasses that inherit from the superclass must explicitly declare the
inheritance relationship. A subclass that inherits from a superclass is automatically
given accessible instance variables and methods defined by the superclass, but is also
free to override superclass methods to define more specific behavior.
For example, a Car superclass class could define general methods common to all
automobiles, but a Ferrari subclass could override the accelerate() method.
Interfaces
A powerful companion to inheritance is the use of interfaces. Interfaces are like a
100-percent abstract superclass that defines the methods a subclass must support, but
not how they must be supported. In other words, an Animal interface might declare
that all Animal implementation classes have an eat() method, but the Animal
interface doesn't supply any logic for the eat() method. That means it's up to the
classes that implement the Animal interface to define the actual code for how that
particular Animal type behaves when its eat() method is invoked.
Finding Other Classes
As we'll see later in the book, it's a good idea to make your classes cohesive. That
means that every class should have a focused set of responsibilities. For instance,
if you were creating a zoo simulation program, you'd want to represent aardvarks
with one class, and zoo visitors with a different class. In addition, you might have
a Zookeeper class, and a Popcorn vendor class. The point is that you don't want a
class that has both Aardvark and Popcorn behaviors (more on that in Chapter 2).
Even a simple Java program uses objects from many different classes: some that
you created, and some built by others (such as Sun's Java API classes). Java organizes
classes into packages, and uses import statements to give programmers a consistent
ch1-1123f.indd 3 11/28/05 12:24:14 AM
评论30
最新资源