Java Reference
In-Depth Information
}
}
When you run this program, it shows all the items being added, but only the
PowerSwitch-
able
ones being switched off:
>
java oo.interfaces.BuildingManagement
Adding RoomLights@2dc77f32
Adding EmergencyLight@2e3b7f32
Adding ComputerCPU@2e637f32
Adding ComputerMonitor@2f1f7f32
Dousing lights in room 101
Dousing monitor at desk 10104
>
Polymorphism/Abstract Methods
Problem
You want each of a number of subclasses to provide its own version of one or more methods.
Solution
Make the method abstract in the parent class; this makes the compiler ensure that each sub-
class implements it.
Discussion
A hypothetical drawing program uses a
Shape
subclass for anything that is drawn.
Shape
has
an abstract method called
computeArea()
that computes the exact area of the given shape:
public abstract class Shape {
protected int x, y;
public abstract double computeArea( );
}