Abstract Methods
An abstract method has no body or implementation code.
public abstract void changeBulb(); // Abstract method
public void changeBulb(); // ILLEGAL: This method requires a body instead of a semicolon
public abstract void changeBulb() { } // ILLEGAL: abstract methods do not specify a body
Concrete subclasses must implement all inherited abstract methods of their superclass.
- A subclass that fails to do so must itself be declared
abstract.
public abstract class LEDLight extends AbstractLight {
}
An abstract class can define non-abstract, or concrete, methods.
public abstract class AbstractLight {
public abstract void changeBulb(); // Abstract method
private int lumens;
public int getLumens() { // Concrete method
return lumens;
}
public void setLumens(int lumens) { // Concrete method
this.lumens = lumens;
}
}
final or private), but are not required to.
Practice Exercise¶
The numeric wrapper classes extend the
abstractclassNumber, which defines bothabstractand concrete methods:public abstract class Number implements java.io.Serializable { public abstract int intValue(); public abstract long longValue(); public abstract float floatValue(); public abstract double doubleValue(); public byte byteValue() { return (byte)intValue(); } public short shortValue() { return (short)intValue(); } //... }
An abstract class is not required to declare any abstract methods.
- It can be subclassed, but not instantiated.