
Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. How would I use interfaces in a useful manner in this program? What use are they in general? MountainBike extends class Bicycle to include state “awesomeness” (first state that popped to mind :rolleyes:)Īt the end, the program prints the states of each bike. It has methods “changeCadence”, “changeGear”, “speedUp”, “applyBrakes”. Bike1 and bike2 are normal Bicycle objects, and bike3 is a MountainBike.īicycle has states “cadence”, “speed”, and “gear”. 3 objects are defined, bike1, bike2, and bike3. There is the superclass Bicycle, and the subclass MountainBike. I have this sample program (attached), which uses bicycles as an example class. I got to the section on interfaces, and for the life of me I cannot understand how to use them, or even what they are. I’m using the Java tutorials on Sun.com to teach myself. I understand classes, and how to use one to define an object. In other words, each class should contain these two methods.I’m trying to teach myself Java for this years competition, having never done anything more complex than Liberty Basic. All classes that descend from TGraphicShape and all classes that descend from TComponent can’t be stored in this list, unless TGraphicShape and TComponent also derived from TStreamable.Even if, say, TGraphicShape has methods with the required signatures, they are not part of the TStreamable hierarchy, so the compiler would not know how to call them, and that is why they can not be allowed.It would be much nicer if the only requirement for a class to stored, no matter from which base class it inherits, is that is must know how to write itself to a stream and read itself from a stream. Any classes that are not descendants of TStreamable can not be allowed, since you would not be able to call WriteToStream or ReadFromStream on them. WriteToStream ( S ) end TStreamable would have two abstract methods, WriteToStream and ReadFromStream, and descendants would override these methods in order to do the streaming.But that would be a little unflexible, since now all classes you could store in that TDataObjectList would have to be descendants of TStreamable.
