Interfaces and Abstract Classes What are they How
Interfaces and Abstract Classes - What are they? - How are they used? - Where will we use them? - Calling superclass’ constructors and methods 1 AP Computer Science A – Healdsburg High School
Abstract Class – A class which at least one of the methods is left “abstract”, or without the code implemented. public abstract class Polygon { private int number. Of. Sides; public Polygon() { /* code not shown */ } public abstract double get. Area(); /* other methods not shown */ } 2 AP Computer Science A – Healdsburg High School
Abstract Class – A class which at least one of the methods is left “abstract”, or without the code implemented. public abstract class Polygon { private int number. Of. Sides; public class Rectangle extends Polygon public { Polygon() { /* code not shown } private double*/my. Length; private double my. Width; public abstract double get. Area(); public Rectangle() /* other methods not/*shown { code */ not shown */ } } public double get. Area() { return my. Length * my. Width; } /* other methods not shown */ } 3 AP Computer Science A – Healdsburg High School
Example: Inheritance Diagram for Grid. World’s Grid Abstract. Grid Bounded. Grid 4 Unbounded. Grid Abstract Grid Class Subclasses of Abstract. Grid AP Computer Science A – Healdsburg High School
Interface – A construct in Java where everything is left “abstract”. Interfaces have no constructors, instance variables, nor program code. public interface Fillable { void fill(int x); int get. Current. Amount(); int get. Maximum. Capacity(); } 5 AP Computer Science A – Healdsburg High School
Interface – A construct in Java where everything is left “abstract”. Interfaces have no constructors, instance variables, nor program code. public interface Fillable { class void public fill(int x); Car implements Fillable { int get. Current. Amount(); . . . int get. Maximum. Capacity(); public void fill(int gallons) } { fuel. Amount += gallons; } public int get. Current. Amount() { return fuel. Amount; } public int get. Maximum. Capacity() { return fuel. Tank. Capacity; } 6 AP Computer Science A – Healdsburg High School }
Interface – A construct in Java where everything is left “abstract”. Interfaces have no constructors, instance variables, nor program code. public interface Fillable { class void public fill(int x); Car implements Fillable { int get. Current. Amount(); . . . int get. Maximum. Capacity(); publicclass void Vending. Machine fill(int gallons) public implements Fillable } fuel. Amount += gallons; } {{. . . public int get. Current. Amount() void fill(int quantity) { fuel. Amount; += quantity; } { return current. Stock } public int get. Maximum. Capacity() int get. Current. Amount() { fuel. Tank. Capacity; { return current. Stock; } } } public int get. Maximum. Capacity() { return 20; } } 7 AP Computer Science A – Healdsburg High School
Example: An Interface used in Grid. World Simulation Grid Interface Grid Abstract. Grid Bounded. Grid Unbounded. Grid Abstract. Grid Class Subclasses of Abstract. Grid A class that implements an Interface MUST implement ALL of the methods defined in the Interface. 8 AP Computer Science A – Healdsburg High School
Example: Our Address Book Program Database Addressbook Interface Database Address. Book implements Database A class that implements an Interface MUST implement ALL of the methods defined in the Interface. 9 AP Computer Science A – Healdsburg High School
Calling Superclass’s Constructors Biped Walker public class Walker extends Biped { // Constructor public Walker(int x, int y, Image left. Pic, Image right. Pic) { Calls Biped’s super(x, y, left. Pic, right. Pic); . . . constructor } The number / types of parameters passed to } super must match parameters of one of the If present, must be the first statement 10 superclass’s constructors. AP Computer Science A – Healdsburg High School
Calling Superclass’s Constructors (cont’d) • One of the superclass’s constructors is always called, but you don’t have to have an explicit super statement. 11 AP Computer Science A – Healdsburg High School
Calling Superclass’s Constructors (cont’d) • Superclass’s constructor calls its superclass’s constructor, and so on, all the way up to Object’s constructor. super( ) super(. . . ) Object Biped Walker 12 AP Computer Science A – Healdsburg High School
Calling Superclass’s Methods 13 Walker Charlie. Chaplin public class Charlie. Chaplin extends Walker {. . . public void next. Step () { turn. Feet. In(); Calls Walker’s super. next. Step(); next. Step turn. Feet. Out(); }. . . super. some. Method refers to some. Method in } the nearest class, up the inheritance line, where some. Method is defined. AP Computer Science A – Healdsburg High School
- Slides: 13