Using Abstract Classes and Interfaces
In this chapter, you find out how to use two similar but subtly distinct features: abstract classes and interfaces. Both let you declare the signatures of the methods and fields that a class implements separately from the class itself. Abstract classes accomplish this by way of inheritance. Interfaces do it without using inheritance, but the effect is similar.
Using Abstract Classes
Java lets you declare that a method or an entire class is abstract, which means that the method has no body. An abstract method is just a prototype for a method: a return type, a name, a list of parameters, and (optionally) a throws clause.
To create an abstract method, you specify the modifier abstract and replace the method body with a semicolon:
public abstract int hit(int batSpeed);
Here the method named hit is declared as an abstract method that returns an int value and accepts an int parameter.
A class that contains at least one abstract method is called an abstract class, and must be declared with the abstract modifier on the class declaration. For example:
public abstract class Ball
{
public abstract int hit(int batSpeed);
}
Warning | If you omit the abstract modifier from the class declaration, the Java compiler coughs up an error message to remind you that the class must be declared abstract. |
An abstract class can't be instantiated. Thus, given the preceding declaration, the following line doesn't compile:
Ball b = new Ball(); // error: Ball is abstract
The problem here isn't with declaring the variable b as a Ball. It's using the new keyword with the Ball class in an attempt to create a Ball object. Because Ball is an abstract class, you can use it to create an object instance.
DESIGN PATTERN | |
You can create a subclass from an abstract class like this:
public class BaseBall extends Ball
{
public int hit(int batSpeed)
{
// code that implements the hit method goes here
}
}
When you subclass an abstract class, the subclass must provide an implementation for each abstract method in the abstract class. In other words, it must override each abstract method with a non-abstract method. (If it doesn't, the subclass is also abstract, so it too cannot be instantiated.)
Tip | Abstract classes are useful when you want to create a generic type that is used as the superclass for two or more subclasses, but the superclass itself doesn't represent an actual object. For example, if all employees are either salaried or hourly, creating an abstract Employee class makes sense, and then use it as the base class for the SalariedEmployee and HourlyEmployee subclasses. |
Here are a few additional points to ponder concerning abstract classes:
-
Not all the methods in an abstract class have to be abstract. A class can provide an implementation for some of its methods but not others. In fact, even if a class doesn't have any abstract methods, you can still declare it as abstract. (In that case, the class can't be instantiated.)
TECHNICAL STAUFF A private method can't be abstract. That only makes sense, because a subclass can't override a private method, and abstract methods must be overridden.
-
Although you can't create an instance of an abstract class, you can declare a variable using an abstract class as its type. Then use the variable to refer to an instance of any of the subclasses of the abstract class.
-
A class can't specify both abstract and final. That would cause one of those logical paradoxes that result in the complete annihilation of the entire universe. Well, hopefully the effect would be localized. But the point is that because an abstract class can only be used if you subclass it, and a final class can't be subclassed, letting you specify both abstract and final for the same class doesn't make sense.
TECHNICAL STAUFF | Abstract classes are used extensively in the Java API. Many of the abstract classes have names that begin with Abstract, such as AbstractBorder, AbstractCollection, and AbstractMap. But most of the abstract classes don't. For example, the InputStream class (used by System.in) is abstract. |
No comments:
Post a Comment