|Table of Contents|
In the lesson titled Object-Oriented Programming Concepts: A Primer you learned the concepts behind object-oriented programming. Now that you have a conceptual understanding of object-oriented programming, it's time to get to work and put those concepts to practical use in Java. This lesson shows you how to use the object-oriented paradigms of the Java language.
In this lesson, you will learn how to create and destroy objects, how to create and subclass classes, how to write methods, how to create and use interfaces, and how to create and use packages. This lesson covers everything from how to protect the innards of an object from other objects, to writing abstract classes and methods, to the use of the root of the Java object hierarchy--the
The Life Cycle of an ObjectAs you learned in Object-Oriented Programming Concepts: A Primer, an object is a software module that has state and behavior. An object's state is contained in its member variables and its behavior is implemented through its methods.
Typically, Java programs that you write will create many objects from prototypes known as classes. These objects interact with one another by sending each other messages. The result of a message is a method invocation which performs some action or modifies the state of the receiving object. Through these object interactions, your Java program can implement a graphical user interface, run an animation, or send and receive information over the network. Once an object has completed the work for which it was created, it is garbage collected and its resources recycled for the use of other objects.
The following pages describe the typical life cycle of an object: 1) creation, 2) use, and 3) destruction.
Creating ClassesA Java object is an instance of a class. Frequently, we say that an object's class is the object's type. The Java environment comes with many classes that you can use in your programs. Or you can write your own. This section shows you how to write your own classes including how to declare member variables and write methods.
Subclasses, Superclasses, and InheritanceThe ability to derive one class from another and inherit its state and behavior is one of object-oriented programming's most powerful paradigms. Inheritance provides a powerful and natural mechanism for organizing and structuring software programs. The most general classes appear higher in the class hierarchy and the most specific classes appear lower in the class hierarchy. In addition, because classes inherit state and behavior from their superclasses you don't have to write that code again--inheritance allows you to reuse code over and over again in each subclass you create.
Creating and Using InterfacesAn interface is a collection of method definitions (without implementations) and constant values. You use interfaces to define a protocol of behavior that can be implemented by any class anywhere in the class hierarchy.
Creating and Using PackagesOnce you've created your classes and interfaces, you can group them together into a package, which is simply a collection of related classes and interfaces. The Java development environment provides several packages of classes that you can use in your Java programs. You can also create your own.
|Table of Contents|