UNI CS 4550 - Comments on Lab 3 & Implications of Inheritance

Unformatted text preview:

Session 6 Comments on Lab 3 & Implications of InheritanceAccumulator ExampleRefactoring AccumulatorAlternative structure of the programCountedAccumulator SolutionSlide 6A solutionLab 3 ExerciseToward a SolutionSlide 10A Problem Accessing Inherited DataA Possible Solution for Accessing Inherited DataA Better Solution for Accessing Inherited DataProgramming with InheritanceSlide 15Slide 16PolymorphismPolymorphic Variables in JavaImplications of Inheritance/PolymorphismTypical Memory LayoutStack-based MemoryConsider Factorial ExampleSession 6Comments on Lab 3&Implications of InheritanceAccumulator Example•a simple calculator app•classes needed:–AdderApp - contains main–AddingFrame - GUI –CloseableFrame - allows X button–Accumulator - internal representation and implementation of the accumulatorRefactoring Accumulator public void plus() { currentSum+=currentNumber; prepareForNextNumber(); } public void minus() { currentSum-=currentNumber; prepareForNextNumber(); } private void prepareForNextNumber() { currentNumber=0; displayNumber=currentSum; } public int getDisplay() { return displayNumber; }} // end class AccumulatorV2public class AccumulatorV2 { private int currentSum; private int currentNumber; private int displayNumber; public Accumulator() { clear(); } public void clear() { currentSum=0; currentNumber=0; displayNumber=0; } public void addDigit( int digit ) { currentNumber=currentNumber*10+digit; displayNumber=currentNumber; }Alternative structure of the program•But another way to structure this program would be to create a relationship which is “wide and shallow”–AdderApp creates an an instance of Accumulator which it passes to an instance of AddingFrame. public class AdderApp { public static void main( String[] args ) { Accumulator a = new Accumulator(); AddingFrame f = new AddingFrame(a); f.show(); } // end main } // end class AdderApp–This is a good example of composition. •We emphasize that AddingFrame is composed of an Accumulator–This is a good example of writing code that is modular.•Now that we know the composition relation, we can compose solutions using variations of Accumulator.CountedAccumulator Solutionpublic class CountedAccumulator extends Accumulator { private int numberOfOperations; public CountedAccumulator() { super(); // calls the superclass’ constructor numberOfOperations=0; } public void plus() { super.plus(); numberOfOperations++; } public void minus() { super.minus(); numberOfOperations++; } public int getOperations() { return numberOfOperations; }} // end class CountedAccumulatorCountedAccumulator Solution•Now, before we can really work with this we need to modify other files in our application. •We need to set up the AddingFrame so that it works with a CountedAccumulator rather than a regular Accumulator. We do this in the AdderApp class for simplicity.Accumulator a = new CountedAccumulator();AddingFrame f = new AddingFrame(a);A solution•Why do we do this in the AdderApp rather than leave it alone and modify the AddingFrame? –Because in the end this makes our AddingFrame slightly more versatile. –Think about it...AddingFrame works with an Accumulator (or CountedAccumulator). If one is provided, it uses it. If one is not provided, it creates it. –THAT, is more versatile than telling an AddingFrame to now always create a CountedAccumulator.Lab 3 ExerciseCreate a class named EvenOddAccumulator that subclasses Accumulator to implement this behavior.EvenOddAccumulators respond to all the same messages as regular Accumulators. But, in response to plus() and minus() messages, an EvenOddAccumulator both computes the new sum and writes a congratulatory message if the sum is even.Toward a SolutionHere is the critical new piece of the EvenOddAccumulator class:if ( currentSum % 2 == 0 ) {System.out.println( "Hurray! You made an even number." );}The big question is, what else is a part of the class?Toward a Solution•Here where I thought you would get into trouble during Lab 3 yesterday…A Problem Accessing Inherited Data$ javac EvenOddAccumulator.javaEvenOddAccumulator.java:17: currentSumhas private access in Accumulatorif ( currentSum % 2 == 0 )^EvenOddAccumulator.java:24: currentSumhas private access in Accumulatorif ( currentSum % 2 == 0 )^2 errorsOops!currentSum is declared as a private instance variable in class Accumulator. private means private: no code outside the Accumulator class can access that variable.A Possible Solution for Accessing Inherited Data•Change currentSum to be public or protected.public class Accumulator {protected int currentSum;...}A Better Solutionfor Accessing Inherited Data(2) Add a protected “accessor” method to theAccumulator class. Use that method to access thecurrentSum instance variable in the subclass.public class Accumulator {...protected int getCurrentSum() {return currentSum;}}Then use getCurrentSum() in EvenOddAccumulator.Programming with InheritanceInheritance is an object-oriented programming construct that enables us to add behavior to an existing system without modifying the existing classes.Programming with InheritanceOur new EvenOddAccumulator class adds behavior to a program that uses Accumulators without modifying:•the behavior of the existing Accumulator class or•the existing AddingFrame class!That means...•No chance of introducing an unnecessary, unexpected errors into the working Accumulator class.•No need to modify programs that use instances of Accumulator but which don’t need instances of EvenOddAccumulator.•The ability to use EvenOddAccumulators in programs that expect to use Accumulators.Programming with InheritanceWe could have achieved some of these results without using inheritance by creating a new class named EvenOddAccumulator that simply duplicated the behavior of existing Accumulator class. Using inheritance means that...•No need to reimplement existing methods.•No need to duplicate code.One of the most important features of object-oriented programming is that it encourages us to create new classes that reuse existing code as much as possible. Without inheritance, you have only one tool for doing that, composition. With inheritance, you have two tools.Polymorphism•polymorphism comes from the Greek root for “many shapes”•polymorphism is about how


View Full Document

UNI CS 4550 - Comments on Lab 3 & Implications of Inheritance

Download Comments on Lab 3 & Implications of Inheritance
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Comments on Lab 3 & Implications of Inheritance and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Comments on Lab 3 & Implications of Inheritance 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?