Sams Teach Yourself Java 2 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Hour 1. Becoming a Programmer
- Hour 2. Writing Your First Program
- Hour 3. Vacationing in Java
- Hour 4. Understanding How Java Programs Work
- Part II: Learning the Basics of Programming
- Hour 5. Storing and Changing Information in a Program
- Hour 6. Using Strings to Communicate
- Hour 7. Using Conditional Tests to Make Decisions
- Hour 8. Repeating an Action with Loops
- Part III: Working with Information in New Ways
- Hour 9. Storing Information with Arrays
- Hour 10. Creating Your First Object
- Hour 11. Describing What Your Object Is Like
- Hour 12. Making the Most of Existing Objects
- Part IV: Programming a Graphical User Interface
- Hour 13. Building a Simple User Interface
- Hour 14. Laying Out a User Interface
- Hour 15. Responding to User Input
- Hour 16. Building a Complex User Interface
- Part V: Creating Multimedia Programs
- Hour 17. Creating Interactive Web Programs
- Hour 18. Handling Errors in a Program
- Hour 19. Creating a Threaded Program
- Hour 20. Reading and Writing Files
- Part VI: Creating Multimedia Programs
- Hour 21. Using Fonts and Color
- Hour 22. Playing Sound Files
- Hour 23. Working with Graphics
- Hour 24. Creating Animation
- Part VII: Appendixes
- Appendix A. Tackling New Features of Java 2 Version 1.4
- Appendix B. Using the Java 2 Software Development Kit
- Appendix C. Programming with the Java 2 Software Development Kit
- Appendix D. Using Sun ONE Studio
- Appendix E. Where to Go from Here: Java Resources
- Appendix F. This Book's Web Site
Workshop: Creating an Object
To see a working example of classes and inheritance, you will create classes that represent two types of objects: cable modems, which will be implemented as the CableModem class, and DSL modems, which will be implemented as the DslModem class.
For the sake of simplicity, the workshop will focus on a few simple attributes and behavior for these objects:
- Each object should have a speed and be able to display it when asked.
- Each object should be able to connect to the Internet.
One thing that cable modems and DSL modems have in common is that they both have a speed. Since it is something they share, it could be put into a class that is the superclass of both the CableModem and DslModem classes. Call this class Modem. Using your word processor, create a new file and save it as Modem.java. Enter Listing 10.2 and save the file.
Example 10.2. The Full Text of Modem.java
1: public class Modem {
2: int speed;
3:
4: public void displaySpeed() {
5: System.out.println("Speed: " + speed);
6: }
7: }
Compile this file with javac or another compiler to produce a file called Modem.class. Although you cannot run this program with the interpreter, you will be able to use it in other classes. You now have a Modem class that can handle one of the things that the CableModem and DslModem classes have in common. By using the extends statement when you are creating the CableModem and DslModem classes, you can make each of them a subclass of Modem.
Start a new file in your word processor and save it as CableModem.java. Enter Listing 10.3 and then save and compile the file.
Example 10.3. The Full Text of CableModem.java
1: public class CableModem extends Modem {
2: String method = "cable connection";
3:
4: public void connect() {
5: System.out.println("Connecting to the Internet ...");
6: System.out.println("Using a " + method);
7: }
8: }
Create a third file with your word processor, and save it as DslModem.java. Enter Listing 10.4 and then save and compile the file when you're done.
Example 10.4. The Full Text of DslModem.java
1: public class DslModem extends Modem {
2: String method = "DSL phone connection";
3:
4: public void connect() {
5: System.out.println("Connecting to the Internet ...");
6: System.out.println("Using a " + method);
7: }
8: }
Once you have compiled all three of these files, you will have three class files: Modem.class, CableModem.class, and DslModem.class. However, you cannot run any of these class files with java or another Java interpreter because they do not have main() blocks. You need to create a short Java program to test out the class hierarchy you have just built.
Return to your word processor and create a new file called TestModems.java. Enter Listing 10.5.
Example 10.5. The Full Text of TestModems.java
1: class TestModems {
2: public static void main(String[] arguments) {
3: CableModem roadRunner = new CableModem();
4: DslModem bellSouth = new DslModem();
5: roadRunner.speed = 500000;
6: bellSouth.speed = 400000;
7: System.out.println("Trying the cable modem:");
8: roadRunner.displaySpeed();
9: roadRunner.connect();
10: System.out.println("Trying the DSL modem:");
11: bellSouth.displaySpeed();
12: bellSouth.connect();
13: }
14: }
Save and compile the file when you're done. When you run it, the output should resemble the following:
Trying the cable modem: Speed: 500000 Connecting to the Internet ... Using a cable connection Trying the DSL modem: Speed: 400000 Connecting to the Internet ... Using a DSL phone connection
The following things are taking place in the program:
- Lines 3–4: Two new objects are created—a CableModem object called roadRunner, and a DslModem object called bellSouth.
- Line 5: The speed variable of the CableModem object named roadRunner is set to 500000.
- Line 6: The speed variable of the DslModem object named bellSouth is set to 400000.
- Line 8: The displaySpeed() method of the roadRunner object is called. This method is inherited from Modem—even though it isn't present in the CableModem class, you can call it.
- Line 9: The connect() method of the roadRunner object is called.
- Line 11: The displaySpeed() method of the bellSouth object is called.
- Line 12: The connect() method of the bellSouth object is called.
Summary | Next Section

Account Sign In
View your cart