Download presentation
Presentation is loading. Please wait.
Published byFrederica Patrick Modified over 9 years ago
1
I NTERFACES, A BSTRACT C LASSES, AND D ATA S TRUCTURES
2
I NTERFACES Let you share methods across multiple (otherwise unrelated) clases Specify new types (like “ IResult ” and “ IContestant ” in your homework assignments) Only have method headers (name of method and its return type), not actual method bodies When a class “ implements ” an interface, that’s a promise to use all of the methods from that interface Are not used to share common method bodies – that’s done with abstract classes
3
A BSTRACT C LASSES Let you share common variables and code (method bodies, not just method names), and to help avoid code duplication. If you have an “Animal” abstract class with a weight and name, and if “Fish” extends that class, Fish then gets a weight and name as well. Classes that “extend” an abstract class will inheret/implement all methods in its superclass. From the example above, if “Animal” has any methods in it, “Fish” gets those methods too. You CANNOT declare an instance of an abstract class If “Animal” is an abstract class, I cannot do: Animal animal = new Animal(5, “animalName”); If “Fish” is NOT an abstract class and is concrete, I can do: Fish fish = new Fish(1, “fishName”); They don’t need to have all methods in an interface that they implement. Abstract classes are exempt from this.
4
H OW MANY CLASSES CAN YOU EXTEND ? Only one. A class can implement multiple interfaces, but it can only extend one class.
5
W HAT IS “ SUPER ”? “super” refers to the class that the current class extends From before, if Fish calls to “super,” it’s calling to the abstract class Animal Let’s say fish have a weight and a name (like all animals do), but then they also have a variable called “scalesColor.” The class for fish would look like: public class Fish extends Animal { String scalesColor; Fish(int weight, String name, String scalesColor){ super(weight, name); this.scalesColor = scalesColor; }
6
L ET ’ S EXAMINE THAT MORE CLOSELY. public class Fish extends Animal { String scalesColor; Fish(int weight, String name, String scalesColor){ super(weight, name); this.scalesColor = scalesColor; } The text in blue refers to the extended abstract class Animal. This means we will go to the constructor for Animal and give it this weight and this name. The abstract class Animal knows what to do with these two pieces of information. The text in red refers to the variable scalesColor, which is within the Fish class and doesn’t exist in the Animal class. The Animal abstract class has no scalesColor, so we can’t pass it into the call to super. Animal just wouldn’t know what to do with it, and you would get an error. The Fish class is what handles the scalesColor.
7
Abstract classes let you share some pieces of code while being able to add in other different pieces of code that are unique to a specific class. In the example below, the text in blue is code that the concrete classes acquire from their abstract class, Animal. Text in red is code that is unique to that specific class. abstract class Animal has: int weight String name class Fish has: int weight String name String scalesColor class Lion has: int weight String name String maneColor int roarScarinessLevel class Goat has: int weight String name String bleatDescription public class Fish extends Animal { String scalesColor; Fish(int weight, String name, String scalesColor){ super(weight, name); this.scalesColor = scalesColor; } public class Lion extends Animal { String maneColor; int roarScarinessLevel; Lion(weight, String name, String maneColor, int roarScarinessLevel){ super(weight, name); this.maneColor = maneColor; this.roarScarinessLevel = roarScarinessLevel; } public class Goat extends Animal { String bleatDescription; Fish(int weight, String name, String bleatDescription){ super(weight, name); this.bleatDescription = bleatDescription; }
8
B INARY S EARCH T REES Each node has two children (left and right) Left’s key is smaller than parent’s key Right’s key is larger than parent’s key
9
E XAMPLE BST Let’s add to this tree and remove from this tree 10 / \ 4 12 / \ 2 7 / \ / \ 1 3 6 8 / 5
10
E XAMPLE BST: ADD E LT addElt( 9 ) 10 / \ 4 12 / \ 2 7 / \ / \ 1 3 6 8 / 5
11
E XAMPLE BST: ADD E LT 9 has been added 10 / \ 4 12 / \ 2 7 / \ / \ 1 3 6 8 / \ 5 9
12
E XAMPLE BST: REM E LT remElt( 6 ) 10 / \ 4 12 / \ 2 7 / \ / \ 1 3 6 8 / \ 5 9
13
E XAMPLE BST: REM E LT 6 has been removed 10 / \ 4 12 / \ 2 7 / \ / \ 1 3 5 8 \ 9
14
E XAMPLE BST: REM E LT remElt( 3 ) 10 / \ 4 12 / \ 2 7 / \ / \ 1 3 5 8 \ 9
15
E XAMPLE BST: REM E LT 3 has been removed 10 / \ 4 12 / \ 2 7 / / \ 1 5 8 \ 9
16
E XAMPLE BST: REM E LT remElt( 4 ) 10 / \ 4 12 / \ 2 7 / / \ 1 5 8 \ 9
17
E XAMPLE BST: REM E LT 4 has been removed… Now what? 10 / \ ? 12 / \ 2 7 / / \ 1 5 8 \ 9
18
E XAMPLE BST: REM E LT We chose the smallest item to the right of where 4 was to be 4’s replacement. 10 / \ 5 12 / \ 2 7 / \ 1 8 \ 9
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.