|
The following is a quick (and incomplete) introduction to inheritance. Its
intended purpose is to give only enough of an explanation to allow us to
continue a discussion about frame windows (a full explanation of inheritance
would take many, many pages).
Recall that a class normally has data values and methods. The methods include
a constructor and some combination of accessor methods (e.g., "getX") and mutator methods (e.g., "setX").
For example, if you are modeling a drinking glass (such as a glass of milk or a glass of juice), you might create the following class:
public class Glass
{
int amount; // amount of fluid, in ounces, in the glass
public Glass()
{
amount = 0;
}
public Glass(int startAmount)
{
amount = startAmount;
}
public void pourIn(int quantity)
{
amount += quantity;
}
public int getAmount()
{
return amount;
}
}
You could use the Glass class in other programs:
public class TestGlass
{
public static void main(String [] args)
{
Glass milk = new Glass();
Glass juice = new Glass(10);
milk.pourIn(6);
milk.pourIn(10);
juice.pourIn(1);
System.out.println("MILK: " + milk.getAmount());
System.out.println("JUICE: " + juice.getAmount());
}
}
Running the TestGlass application yields this output:
MILK: 16
JUICE: 11
After using the Glass class for a while, you might decide that you want to
make some improvements to that class. There are two ways of adding to the
Glass class. One way is to simply modify the Glass class itself. The other way is to use inheritance. With inheritance you create a new class that says it is going to inherit all of the original class (the original class' data and methods) and add to that to create a new, improved class. For example, you might want to add a print method:
public class MyGlass extends Glass
{
public MyGlass ()
{
super();
}
public MyGlass(int startAmount)
{
super(startAmount);
}
public void print()
{
System.out.println("This glass contains " + amount +
" ounces.");
}
}
Normally you would make Glass' "amount" variable private and only access its
contents via methods such as "getAmount", but I wanted to illustrate how
the "amount" variable is part of any new MyGlass objects. MyGlass objects will
also have "pourIn" and "getAmount" methods. Every class needs one or more
constructors -- our MyGlass constructors simply call the Glass constructors via
"super" ("super" is how a subclass refers to a superclass' constructor).
The main thing we've done here is add a new method named "print". So in addition to the constructors, MyGlass objects will have the following three methods:
print, pourIn, and getAmount. Other Java applications can make use of both the Glass class and the improved MyGlass class:
public class TestGlass2
{
public static void main(String [] args)
{
Glass milk = new Glass();
Glass juice = new Glass(10);
MyGlass coffee = new MyGlass(7);
coffee.pourIn(4);
coffee.pourIn(3); // using Glass' pourIn
coffee.print(); // using new MyGlass print
milk.pourIn(6);
milk.pourIn(10);
juice.pourIn(1);
System.out.println("MILK: " + milk.getAmount());
System.out.println("JUICE: " + juice.getAmount());
}
}
Running this program gives the following output:
This glass contains 14 ounces.
MILK: 16
JUICE: 11
In addition to using inheritance to add new methods, you can override (replace) existing methods with improved versions of those methods. In the original version of the Glass class the "pourIn" method does not check if the quantity to be added is a positive number (it does not make sense to "pour in" a negative amount of fluid). The following enhanced version of Glass, called BetterGlass, replaces pourIn with a better version of the method (again, the style here is not the best -- this example
is only focusing on the ability to override a method and NOT on how you should approach inheritance in general):
public class BetterGlass extends Glass
{
public BetterGlass ()
{
super();
}
public BetterGlass(int quantity)
{
super(quantity);
}
/** This method overrides the pourIn in the super class Glass. */
public void pourIn(int quantity)
{
// Leave the amount in the glass unchanged if the
// user attempts to pour in a negative quantity.
if (quantity >= 0)
amount += quantity;
}
}
Here is an application that uses both the Glass and BetterGlass classes:
public class TestGlass3
{
public static void main(String [] args)
{
Glass tea = new Glass(3);
tea.pourIn(1);
tea.pourIn(-10); // ?!
System.out.println("Tea amount: " + tea.getAmount() );
BetterGlass pop = new BetterGlass(3);
pop.pourIn(1);
pop.pourIn(-10); // improved version of pourIn ignores -10!
System.out.println("Pop amount: " + pop.getAmount() );
}
}
The above code yields the following output:
Tea amount: -6
Pop amount: 4
In the next section we look at using inheritance to customize the JFrame class to add buttons, text fields, checkboxes, etc.
|