Previous Next |
Workshop: Creating an ObjectTo 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:
One thing that cable modems and DSL modems have in common is that they both have a speed. Because 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. Listing 10.2. The Full Text of Modem.java1: 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. Listing 10.3. The Full Text of CableModem.java1: 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. Listing 10.4. The Full Text of DslModem.java1: 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. Listing 10.5. The Full Text of TestModems.java1: 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:
|
Previous Next |