To see a working example of classes and inheritance, in the next project you create classes that represent two types of objects: cable modems, which are implemented as the CableModem
class, and DSL modems, which are implemented as the DslModem
class. The workshop focuses on simple attributes and behavior for these objects:
• Each object should have a speed that it can display.
• 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. Because this is something they share, it can be put into a class that is the superclass of both the CableModem
and DslModem
classes. Call this class Modem
. In NetBeans, create a new empty Java class called Modem
. Enter Listing 10.2 in the source editor and save the file.
1: public class Modem {
2: int speed;
3:
4: public void displaySpeed() {
5: System.out.println("Speed: " + speed);
6: }
7: }
This file is compiled automatically as Modem.class
. You cannot run this program directly, but you can use it in other classes. The Modem
class 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 empty Java file in NetBeans with the class name CableModem
. Enter Listing 10.3 and save the file.
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 in NetBeans named DslModem
. Enter Listing 10.4 and save the file.
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: }
If there were no errors, you now have three class files: Modem.class
, CableModem.class
, and DslModem.class
. However, you cannot run any of these class files because they do not have main()
blocks like the ones in other programs you’ve created. You need to create a short application to test out the class hierarchy you have just built.
Return to your NetBeans and create a new empty Java file with the class name ModemTester
. Enter Listing 10.5 in the source editor and save the file.
1: public class ModemTester {
2: public static void main(String[] args) {
3: CableModem surfBoard = new CableModem();
4: DslModem gateway = new DslModem();
5: surfBoard.speed = 500000;
6: gateway.speed = 400000;
7: System.out.println("Trying the cable modem:");
8: surfBoard.displaySpeed();
9: surfBoard.connect();
10: System.out.println("Trying the DSL modem:");
11: gateway.displaySpeed();
12: gateway.connect();
13: }
14: }
When you run the program, you should see output matching Figure 10.4.
The following things are taking place in Listing 10.5:
• Lines 3–4: Two new objects are created—a CableModem
object called surfBoard
and a DslModem
object called gateway
.
• Line 5: The speed
variable of the CableModem
object named surfBoard
is set to 500000
.
• Line 6: The speed
variable of the DslModem
object named gateway
is set to 400000
.
• Line 8: The displaySpeed()
method of the surfBoard
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 surfBoard
object is called.
• Line 11: The displaySpeed()
method of the gateway
object is called.
• Line 12: The connect()
method of the gateway
object is called.
After creating your first class of objects and arranging several classes into a hierarchy, you ought to be more comfortable with the term object-oriented programming (OOP). You learn more about object behavior and attributes in the next two hours as you start creating more sophisticated objects.
Terms such as program, class, and object make more sense as you become more experienced with this style of development. OOP is a concept that takes some time to get used to. When you have mastered it, you find that it’s an effective way to design, develop, and debug computer programs.
Q. Can classes inherit from more than one class?
A. It’s possible with some programming languages (such as C++), but not Java. Multiple inheritance is a powerful feature, but it also makes OOP a bit harder to learn and use. Java’s developers decided to limit inheritance to one superclass for any class, although a class can have numerous subclasses. One way to compensate for this limitation is to inherit methods from a special type of class called an interface. You learn more about interfaces during Hour 19, “Creating a Threaded Program.”
Q. When would you want to create a method that isn’t public
?
A. The main time you would not want to make a method available to other programs is when the method is strictly for the use of one program you’re writing. If you’re creating a game program and your shootRayGun()
method is highly specific to the game you’re writing, it could be a private method. To keep a method from being public
, leave off the public
statement in front of the method’s name.
Q. Why is it possible to use char
values as if they were int
values?
A. A character can be used as an int
variable because each character has a corresponding numeric code that represents its position in the character set. If you have a variable named k
with the value 67, the cast (char) k
produces the character value ‘C’
because the numeric code associated with a capital C is 67, according to the ASCII character set. The ASCII character set is part of the Unicode character standard adopted by the Java language.
Q. Does Tabasco hot sauce spoil?
A. No it doesn’t, though if you keep an opened bottle around for several years it will change color. The ingredients of vinegar, red pepper, and salt are an extremely inhospitable environment for bacterial growth.
McIlhenny Company, the makers of Tabasco, say the original brand has a shelf life of five years. Other versions have a shelf life from 18 months to three years.
As a huge fan of the product, I find it hard to believe anyone is keeping a bottle of Tabasco around long enough to ask this question.
The following questions test your knowledge of objects and the programs that use them.
1. What statement is used to enable one class to inherit from another class?
A. inherits
B. extends
C. handItOverAndNobodyGetsHurt
2. Why are compiled Java programs saved with the .class
file extension?
A. Java’s developers think it’s a classy language.
B. It’s a subtle tribute to the world’s teachers.
C. Every Java program is a class.
3. What are the two things that make up an object?
A. Attributes and behavior
B. Commands and data files
C. Spit and vinegar
1. B. The extends
statement is used because the subclass is an extension of the attributes and behavior of the superclass and of any superclasses above that in the class hierarchy.
2. C. Your programs are always made up of at least one main class and any other classes that are needed.
3. A. In a way, B also is true because commands are comparable to behavior, and data files are analogous to attributes.
If you don’t object, you can extends
your knowledge of this hour’s topics with the following activities:
• Create an AcousticModem
class with a speed of 300 and its own connect()
method.
• Add a disconnect()
method to one of the classes in the Modem
project, deciding where it should go to support modem disconnection in cable, DSL, and acoustic modems.
To see Java programs that implement these activities, visit the book’s website at www.java24hours.com.
3.17.183.186