Presentation is loading. Please wait.

Presentation is loading. Please wait.

AP Computer Science A – Healdsburg High School 1 Interfaces and Abstract Classes - What are they? - How are they used? - Where will we use them? - Calling.

Similar presentations


Presentation on theme: "AP Computer Science A – Healdsburg High School 1 Interfaces and Abstract Classes - What are they? - How are they used? - Where will we use them? - Calling."— Presentation transcript:

1 AP Computer Science A – Healdsburg High School 1 Interfaces and Abstract Classes - What are they? - How are they used? - Where will we use them? - Calling superclass’ constructors and methods

2 AP Computer Science A – Healdsburg High School 2 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 numberOfSides; public Polygon() {/* code not shown */} public abstract double getArea(); /* other methods not shown */ }

3 AP Computer Science A – Healdsburg High School 3 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 numberOfSides; public Polygon() {/* code not shown */} public abstract double getArea(); /* other methods not shown */ } public class Rectangle extends Polygon { private double myLength; private double myWidth; public Rectangle() {/* code not shown */} public double getArea() { return myLength * myWidth; } /* other methods not shown */ }

4 AP Computer Science A – Healdsburg High School 4 Example: Inheritance Diagram for GridWorld’s Grid AbstractGrid BoundedGridUnboundedGrid Abstract Grid Class Subclasses of AbstractGrid

5 AP Computer Science A – Healdsburg High School 5 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 getCurrentAmount(); int getMaximumCapacity(); }

6 AP Computer Science A – Healdsburg High School 6 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 getCurrentAmount(); int getMaximumCapacity(); } public class Car implements Fillable {... public void fill(int gallons) {fuelAmount += gallons;} public int getCurrentAmount() {return fuelAmount;} public int getMaximumCapacity() {return fuelTankCapacity;} }

7 AP Computer Science A – Healdsburg High School 7 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 getCurrentAmount(); int getMaximumCapacity(); } public class Car implements Fillable {... public void fill(int gallons) {fuelAmount += gallons;} public int getCurrentAmount() {return fuelAmount;} public int getMaximumCapacity() {return fuelTankCapacity;} } public class VendingMachine implements Fillable {... public void fill(int quantity) {currentStock += quantity;} public int getCurrentAmount() {return currentStock;} public int getMaximumCapacity() {return 20;} }

8 AP Computer Science A – Healdsburg High School 8 Example: An Interface used in GridWorld Simulation A class that implements an Interface MUST implement ALL of the methods defined in the Interface. AbstractGrid BoundedGridUnboundedGrid AbstractGrid Class Subclasses of AbstractGrid Grid Grid Interface

9 AP Computer Science A – Healdsburg High School 9 Example: Our Address Book Program Database Addressbook Interface Database AddressBook implements Database A class that implements an Interface MUST implement ALL of the methods defined in the Interface.

10 AP Computer Science A – Healdsburg High School 10 Calling Superclass’s Constructors public class Walker extends Biped { // Constructor public Walker(int x, int y, Image leftPic, Image rightPic) { super(x, y, leftPic, rightPic);... } Biped Walker Calls Biped’s constructor If present, must be the first statement The number / types of parameters passed to super must match parameters of one of the superclass’s constructors.

11 AP Computer Science A – Healdsburg High School 11 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.

12 AP Computer Science A – Healdsburg High School 12 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. Biped Walker Object super( ) super(...)

13 AP Computer Science A – Healdsburg High School 13 Calling Superclass’s Methods public class CharlieChaplin extends Walker {... public void nextStep () { turnFeetIn(); super.nextStep(); turnFeetOut(); }... } Walker CharlieChaplin Calls Walker’s nextStep super.someMethod refers to someMethod in the nearest class, up the inheritance line, where someMethod is defined.


Download ppt "AP Computer Science A – Healdsburg High School 1 Interfaces and Abstract Classes - What are they? - How are they used? - Where will we use them? - Calling."

Similar presentations


Ads by Google