i nterfaces, a bstract c lasses, and d ata s tructures

18
INTERFACES, ABSTRACT CLASSES, AND DATA STRUCTURES

Upload: frederica-patrick

Post on 17-Jan-2018

218 views

Category:

Documents


0 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: I NTERFACES, A BSTRACT C LASSES, AND D ATA S TRUCTURES

INTERFACES, ABSTRACT CLASSES, AND DATA STRUCTURES

Page 2: I NTERFACES, A BSTRACT C LASSES, AND D ATA S TRUCTURES

INTERFACES 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

Page 3: I NTERFACES, A BSTRACT C LASSES, AND D ATA S TRUCTURES

ABSTRACT CLASSES 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.

Page 4: I NTERFACES, A BSTRACT C LASSES, AND D ATA S TRUCTURES

HOW MANY CLASSES CAN YOU EXTEND?

Only one. A class can implement multiple interfaces, but it can only extend one class.

Page 5: I NTERFACES, A BSTRACT C LASSES, AND D ATA S TRUCTURES

WHAT 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;

}

Page 6: I NTERFACES, A BSTRACT C LASSES, AND D ATA S TRUCTURES

LET’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.

Page 7: I NTERFACES, A BSTRACT C LASSES, AND D ATA S TRUCTURES

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 weightString name

class Fish has:int weight

String nameString scalesColor

class Lion has:int weight

String nameString maneColor

int roarScarinessLevel

class Goat has:int weight

String nameString 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;

}

Page 8: I NTERFACES, A BSTRACT C LASSES, AND D ATA S TRUCTURES

BINARY SEARCH TREES 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

Page 9: I NTERFACES, A BSTRACT C LASSES, AND D ATA S TRUCTURES

EXAMPLE BST Let’s add to this tree and remove from this

tree         10       /    \      4      12    /   \   2     7 /  \   /  \1   3  6    8      /     5

Page 10: I NTERFACES, A BSTRACT C LASSES, AND D ATA S TRUCTURES

EXAMPLE BST: ADDELT addElt(9)

         10       /    \      4      12    /   \   2     7 /  \   /  \1   3  6    8      /     5

Page 11: I NTERFACES, A BSTRACT C LASSES, AND D ATA S TRUCTURES

EXAMPLE BST: ADDELT 9 has been added

         10       /    \      4      12    /   \   2     7 /  \   /  \1   3  6    8      / \     5 9

Page 12: I NTERFACES, A BSTRACT C LASSES, AND D ATA S TRUCTURES

EXAMPLE BST: REMELT remElt(6)

         10       /    \      4      12    /   \   2     7 /  \   /  \1   3  6    8      / \     5 9

Page 13: I NTERFACES, A BSTRACT C LASSES, AND D ATA S TRUCTURES

EXAMPLE BST: REMELT 6 has been removed

         10       /    \      4      12    /   \   2     7 /  \   /  \1   3  5    8       \      9

Page 14: I NTERFACES, A BSTRACT C LASSES, AND D ATA S TRUCTURES

EXAMPLE BST: REMELT remElt(3)

         10       /    \      4      12    /   \   2     7 /  \   /  \1   3  5    8       \      9

Page 15: I NTERFACES, A BSTRACT C LASSES, AND D ATA S TRUCTURES

EXAMPLE BST: REMELT 3 has been removed

         10       /    \      4      12    /   \   2     7 /      /  \1      5    8       \      9

Page 16: I NTERFACES, A BSTRACT C LASSES, AND D ATA S TRUCTURES

EXAMPLE BST: REMELT remElt(4)

         10       /    \      4      12    /   \   2     7 /      /  \1      5    8       \      9

Page 17: I NTERFACES, A BSTRACT C LASSES, AND D ATA S TRUCTURES

EXAMPLE BST: REMELT 4 has been removed… Now what?

         10       /    \      ?      12    /   \   2     7 /      /  \1      5    8       \      9

Page 18: I NTERFACES, A BSTRACT C LASSES, AND D ATA S TRUCTURES

EXAMPLE BST: REMELT 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