The Java Tutorials (Object-Oriented Programming Concepts)

Start from the beginning
                                        

contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class

claims to implement an interface, all methods defined by that interface must appear in its source code before the class will

successfully compile.

What Is a Package?

A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being

similar to different folders on your computer. You might keep HTML pages in one folder, images in another, and scripts or

applications in yet another. Because software written in the Java programming language can be composed of hundreds or thousands of

individual classes, it makes sense to keep things organized by placing related classes and interfaces into packages.

The Java platform provides an enormous class library (a set of packages) suitable for use in your own applications. This library

is known as the "Application Programming Interface", or "API" for short. Its packages represent the tasks most commonly associated

with general-purpose programming. For example, a String object contains state and behavior for character strings; a File object

allows a programmer to easily create, delete, inspect, compare, or modify a file on the filesystem; a Socket object allows for the

creation and use of network sockets; various GUI objects control buttons and checkboxes and anything else related to graphical

user interfaces. There are literally thousands of classes to choose from. This allows you, the programmer, to focus on the design

of your particular application, rather than the infrastructure required to make it work.

The Java Platform API Specification contains the complete listing for all packages, interfaces, classes, fields, and methods

supplied by the Java Platform 6, Standard Edition. Load the page in your browser and bookmark it. As a programmer, it will become

your single most important piece of reference documentation.

Use the questions and exercises presented in this section to test your understanding of objects, classes, inheritance, interfaces,

and packages.

Questions

Real-world objects contain ___ and ___.

A software object's state is stored in ___.

A software object's behavior is exposed through ___.

Hiding internal data from the outside world, and accessing it only through publicly exposed methods is known as data ___.

A blueprint for a software object is called a ___.

Common behavior can be defined in a ___ and inherited into a ___ using the ___ keyword.

A collection of methods with no implementation is called an ___.

A namespace that organizes classes and interfaces by functionality is called a ___.

The term API stands for ___?

Exercises

Create new classes for each real-world object that you observed at the beginning of this trail. Refer to the Bicycle class if you

forget the required syntax.

For each new class that you've created above, create an interface that defines its behavior, then require your class to implement

it. Omit one or two methods and try compiling. What does the error look like?

Check your answers

Answers to Questions

Real-world objects contain state and behavior.

A software object's state is stored in fields.

A software object's behavior is exposed through methods.

Hiding internal data from the outside world, and accessing it only through publicly exposed methods is known as data

encapsulation.

A blueprint for a software object is called a class.

Common behavior can be defined in a superclass and inherited into a subclass using the extends keyword.

A collection of methods with no implementation is called an interface.

A namespace that organizes classes and interfaces by functionality is called a package.

The term API stands for Application Programming Interface.

Answers to Exercises

Your answers will vary depending on the real-world objects that you are modeling.

Your answers will vary here as well, but the error message will specifically list the required methods that have not been implemented.

You've reached the end of published parts.

⏰ Last updated: May 23, 2009 ⏰

Add this story to your Library to get notified about new parts!

The Java Tutorials (Object-Oriented Programming Concepts)Where stories live. Discover now