1 / 13

Abstract Class – A class which at least one of the methods

Interfaces and Abstract Classes - What are they? - How are they used? - Where will we use them? - Calling superclass’ constructors and methods . Abstract Class – A class which at least one of the methods is left “abstract”, or without the code implemented. public abstract class Polygon {

sage
Download Presentation

Abstract Class – A class which at least one of the methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Interfaces and Abstract Classes- What are they?- How are they used?- Where will we use them?- Calling superclass’ constructors and methods

  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. 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. Example: Inheritance Diagram for GridWorld’s Grid Abstract Grid Class AbstractGrid BoundedGrid UnboundedGrid Subclasses of AbstractGrid

  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. 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. 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. Example: An Interface used in GridWorld Simulation Grid Interface Grid AbstractGrid Class AbstractGrid BoundedGrid UnboundedGrid Subclasses of AbstractGrid A class that implements an Interface MUST implement ALL of the methods defined in the Interface.

  9. Example: Our Address Book Program Database Interface Database Addressbook AddressBook implements Database A class that implements an Interface MUST implement ALL of the methods defined in the Interface.

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

  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. 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. Object super( ) Biped super(...) Walker

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

More Related