Development by Extension Rather than build everything from scratch every time, existing classes should be re-used as much as possible. But existing classes may not quite do what you want them to. Extending an existing class will allow you to make it do exactly what you want it to. Developing a class hierarchy from the start makes it more likely that you will find an existing class suitable to extend.
Managing Complexity Building five small classes in a hierarchy is simpler that building one large class (although it may not seem so at first!). a b c d e f a b c de f
Design, build, test... Production can be divided into a number of stages. In each stage a class is designed, built and tested before next class in the hierarchy is started. a design build test b design build test c design build test d design build test
The Counters hierarchy The Counters hierarchy supplies the functionality of a hand held counter. The BasicCounter supplies the essential counting behaviour, the LimitedCounter adds limits to the range it can count in and the three *Counter classes provide specialised counting behaviour. BasicCounter LimitedCounter RolloverCounterStoppingCounterWarningCounter RoomMonitorMoneyRegister
BasicCounter class diagram Object BasicCounter number CountedIs BasicCountercountunCountreset set CountTo initialCount occurrencessetTo theInitialCount counted package counters
BasicCounter header 0001 // Filename counters/BasicCounter.java // Root class of the Counters hierarchy providing the 0003 // essential counting functionality // 0005 // This version written for SDO lecture // Fintan Culwin, v0.1, February package counters; public class BasicCounter extends Object { Object BasicCounter package counters
BasicCounter - data attributes private int counted = 0; 0013 private int theInitialCount = 0; 0014 These are encapsulated instance attributes, of the primitive int type. Every instance of the class will have their own copy of them. theInitialCount counted
BasicCounter - constructors 0015 public BasicCounter() { 0016 this( 0); 0017 } // End default constructor public BasicCounter( int initialCount) { 0020 counted = initialCount; 0021 theInitialCount = initialCount; 0022 } // End alternative constructor. The purpose of a constructor is to place the new instance of the class into a well defined initial state. (This usually means setting the state of the instance attributes.) BasicCounter initialCount
BasicCounter - counting methods 0024 public void count() { 0025 counted++; 0026 } // End count public void unCount() { 0029 counted--; 0030 } // end unCount public void reset() { 0033 counted = theInitialCount; 0034 } // End numberCountedIs. These methods change the state of the instance by changing the values of its instance attributes. countunCountreset
BasicCounter - inquiry method 0036 public int numberCountedIs() { 0037 return counted; 0038 } // End numberCountedIs. This is an inquiry method which return s some information about the internal state of the instance. number CountedIs occurrences
BasicCounter - setCountTo() method 0040 protected void setCountTo( int setTo) { 0041 counted = setTo; 0042 } // End setCountTo } // End BasicCounter. This protected method can only be seen, and so used, by other classes in the same package of classes. set CountTo setTo
BasicCounterDemo - instance diagram Instance diagrams show the roles and relationships relationships between the class instances which make up a program. main instance of BasicCounterDemo demoCounter instance of BasicCounter demonstrates is a Object
BasicCounterDemo - constructors 0008 package counters; class BasicCounterDemonstration { public static void main( String argv[]) { BasicCounter aCounter; System.out.println( "\n\n\t Basic Counter Demonstration"); System.out.print( "\n\nConstructing an instance with “); 0019 System.out.println( "the initial value 4."); 0020 aCounter = new BasicCounter( 4); 0021 System.out.print( "Instance created... "); Basic Counter Demonstration Constructing an instance with the initial value 4. Instance created...
BasicCounterDemo - NumberCountedIs() System.out.print( "\n\nDemonstrating numberCountedIs()” “, it should be 4... "); 0026 System.out.println( aCounter.numberCountedIs()); 0027 Demonstrating numberCountedIs(), it should be
BasicCounterDemo - counting methods 0028 System.out.println( "\n\nDemonstrating count()."); 0029 aCounter.count(); 0030 System.out.print( "Showing the changed value, it should be 5... "); 0031 System.out.println( aCounter.numberCountedIs()); System.out.println( "\n\nDemonstrating unCount(). "); 0034 aCounter.unCount(); 0035 System.out.print( "Showing the changed value, it should be 4... "); 0036 System.out.println( aCounter.numberCountedIs()); Demonstrating count(). Showing the changed value, it should be Demonstrating unCount(). Showing the changed value, it should be 4...4
BasicCounterDemo - setCountTo() 0038 System.out.println( "\n\nDemonstrating setCountTo()”+ “, setting to 10."); 0039 aCounter.setCountTo( 10); 0040 System.out.print( "Showing the changed value, “ + “it should be "); 0041 System.out.println( aCounter.numberCountedIs()); As setCountTo() is protected, it can only be demonstrated if it is temporarily made public. Demonstrating setCountTo(), setting to 10. Showing the changed value, it should be
BasicCounterDemo - reset() 0043 System.out.println( "\n\nDemonstrating reset()."); 0044 aCounter.reset(); 0045 System.out.print( "Showing the reset value, it should be 4... "); 0046 System.out.println( aCounter.numberCountedIs()); Demonstrating reset(). Showing the reset value, it should be
BasicCounterDemo - second instance 0049 System.out.print( "\n\nConstructing a new instance “); 0050 System.out.println( “with the default value 0."); 0051 aCounter = new BasicCounter(); 0052 System.out.println( "New instance created..."); 0053 System.out.print( "\n\nShowing the value of the new instance,“); 0054 System.out.print( "it should be 0... "); 0055 System.out.println( aCounter.numberCountedIs()); System.out.println( "\n\nDemonstration finished.\n\n"); 0058 } // End main } // End class BasicCounterDemonstration. Constructing a new instance with the default value 0. New instance created... Showing the value of the new instance, it should be Demonstration finished.