Section 7.4. Concepts Summary


[Page 245 (continued)]

7.4. Concepts Summary

In this chapter we discussed packages, using predefined Java classes, inheritance, and interfaces.

7.4.1. Packages

A package is a group of related classes. Classes that are basic to the Java language are in java.lang like the System class. Classes that are used for graphics are in java.awt like the Color class. Classes that are used for input and output are in java.io like the File class.

The full name for a class is packageName.ClassName. You don't have to use the full name for any class in the package java.lang. You don't have to use the full name for classes in packages other than java.lang if you use a import statement. You can import a class by using the keyword import and then the full name of the class.

> import java.awt.Color;


You can also import all classes in a package.

> import java.awt.*;


When you use classes in packages other than java.lang in methods you will either need to use the full class name or add import statements before the class definition.

import java.awt.*; import java.awt.font.*; import java.awt.geom.*; import java.text.*; /**  * A class that represents a picture.   This class inherits from  * SimplePicture and allows the student to add functionality to  * the Picture class.  *  * Copyright Georgia Institute of Technology 2004  * @author Barbara Ericson ericson@cc.gatech.edu  */ public class Picture extends SimplePicture



[Page 246]

7.4.2. Predefined Java Classes

There are a large number of predefined classes in Java. The API contains the documentation for all the classes in a version of Java. The documentation tells you important information about each class such as what methods it has. The documentation is organized by packages. There are classes for doing graphical user interfaces (in packages javax.swing and java.awt). There are classes for doing input and output (in package java.io). There are classes for doing work with databases (in package java.sql). There are classes for doing work with sound (in package javax.sound). There are classes for working with lists, sets, and maps (java.util).

It is easier and faster to build on what exists rather than write your own classes. Even if what you need isn't part of Java, someone else may have written something similar.

7.4.3. Inheritance

When one class inherits from another it gets all the data (fields) and behavior (methods) from that class. This means that if Graphics2D inherits from Graphics it understands the same public messages as Graphics. The API for a class shows what class it inherits from as well as all methods that are inherited and what class they are inherited from.

7.4.4. Interfaces

A Java class can inherit from only one class, but it can implement several interfaces. An interface defines how classes will communicate without worrying about what types they actually are. An object of a class that implements an interface can be declared with the interface name as the type. So if a GradientPaint object implements the Paint interface it can be passed as a parameter to methods that take objects of the type Paint. The API for a class shows what interfaces it implements.



Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
ISBN: N/A
EAN: N/A
Year: 2007
Pages: 191

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net