Download presentation
Presentation is loading. Please wait.
Published byCurtis Fox Modified over 9 years ago
1
1 Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com Day 4
2
2 Sometimes we want to do several tasks, but the tasks are very similar. How can we build the classes to take advantage of the common parts of the task and yet distinguish the specific differences? Another way to say that is, how can we design the inheritance tree so that we don’t duplicate common code used among sub-classes, yet allow sub-classes to have some specific differences? The answer is: use an abstract class…
3
3 Here is a task for a team of robots. We want to lay down beepers in a 5-by-4 field. The odd-numbered rows have 2 beepers per street corner, the even have 3.
4
4 Here is how we’d organize that with what we currently know: UrRobot TwoRowLayerThreeRowLayer layBeepers() putBeepers() discuss problems with design
5
5 On the previous slide, we saw that layBeepers() would have the exact same implementation for both types of beeper layers - namely: public void layBeepers() { move(); putBeepers(); move(); putBeepers(); move(); putBeepers(); move(); putBeepers(); move(); } discuss why code duplication (a.k.a., copy/paste) and lack of localization are poor/costly design patterns
6
6 At the same time, we saw that putBeepers() would have a different implementation in each of the subclasses (one puts 2, the other puts 3). So here is the new design pattern: We’ll extract out an abstract concept of what a general beeper layer would look like and put that into a class (in this case, an abstract class). Methods in the abstract class that have the exact same implementation regardless of the subclass will be implemented in the abstract class. Methods that would have different implementations in the subclasses will not be implemented in the abstract class, forcing each subclass to give its own unique implementation…
7
7 TwoRowLayer public void putBeepers() { … } UrRobot BeeperLayer public void layBeepers() { … } public abstract void putBeepers(); ThreeRowLayer public void putBeepers() { … }
8
8 public abstract class BeeperLayer extends UrRobot { public BeeperLayer(int street, int avenue, Direction direction, int beepers) { super(street, avenue, direction, beepers); } // The following abstract method will be used by TwoRowLayer and ThreeRowLayer public abstract void putBeepers(); public void layBeepers() { move(); putBeepers(); move(); putBeepers(); move(); putBeepers(); move(); putBeepers(); move(); } public class TwoRowLayer extends BeeperLayer { public TwoRowLayer( int st, int av, Direction dir, int beeps) { super(st, av, dir, beeps); } public void putBeepers() { putBeeper(); }
9
9 public class BeeperLayerTester implements Directions { public static void main(String[] args) { BeeperLayer lisa; lisa = new TwoRowLayer(1, 3,East, infinity); lisa.layBeepers(); lisa = new ThreeRowLayer(2, 3, East, infinity); lisa.layBeepers(); lisa = new TwoRowLayer(3, 3, East, infinity); lisa.layBeepers(); lisa = new ThreeRowLayer(4, 3, East, infinity); lisa.layBeepers(); lisa = new TwoRowLayer(5, 3, East, infinity); lisa.layBeepers(); } } abstraction, abstract class, abstract method, polymorphism making references to the code, the inheritance tree, or whatever else we just discussed in the BeeperLayer problem, pick one of these terms and demonstrate that you know what it means
10
10 Abstraction - Mechanism and practice to reduce and factor out details so that one can focus on a few concepts at a time (i.e. abstracting common code and putting it into a method) Abstract class – Special class used to provide common code used among sub-classes, yet allow sub-classes to have some specific differences. It is not possible to create an instance of an abstract class (it must be extended and then that class can be instantiated). An abstract class that only contains abstract methods is an interface (we’ll learn about that later). Abstract method – Method in an abstract class that must be redefined in a class that extends the abstract class. Polymorphism – Objects in different classes can behave differently when sent the same message. In practical terms, polymorphism means that if class B inherits from class A, it doesn’t have to inherit everything about class A; it can do some of the things that class A does differently.
11
11 Initialize – Set the value of a variable to a specific value For example: karel = new UrRobot(1, 2, North, 0); Assignment – Used to change the object to which an reference points. Use the equal sign (=) to denote assignment. Reference (or Variable) – name used to identify a field (i.e. the robot name “karel” is a reference or variable). Instance Variable (or Field) – The things that an object remembers. These are the variables defined in the object class. null – Special value to indicate that the reference to the variable does not refer to anything at all (i.e. UrRobot karel = null). Parameter – Information sent to a method. When we instantiate a new robot object, we are passing 4 different parameters which are used by the constructor method. Parameters can be of any type.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.