1 / 8

Intro to CS – Honors I Interfaces and Abstract Classes

Intro to CS – Honors I Interfaces and Abstract Classes. Georgios Portokalidis gportoka@stevens.edu. What I s an Interface?. A point where two systems (objects) meet and interact or the portion of a class that tells a programmer how to use it An interface of a class consists of

sulwyn
Download Presentation

Intro to CS – Honors I Interfaces and Abstract Classes

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. Intro to CS – Honors IInterfaces and Abstract Classes Georgios Portokalidis gportoka@stevens.edu

  2. What Is an Interface? • Apoint where two systems (objects) meet and interact or the portion of a class that tells a programmer how to use it • An interface of a class consists of • Its methods headings • Its public attributesand constants • Its comment, including pre- and post-conditions • A class’ interface is normally part of the class • Java allows us to separately define an interface of a class • How an interface is implemented can differ

  3. An Interface for a Pet Class /** Sets a pets name to petName. */ public void setName(String petName) /** Returns true if a pet eats the given food.*/ public boolean eat(String food) /** Returns a description of a pet’s response to the given command. */ public String respond(String command) Any object of a class providing a particular interface guarantees we can call a method of the interface This invocation is always valid String response = myPet.respond("Come!");

  4. Java Interfaces • A Java interface contains the headings for a number of public methods • Should also include comments so developers implementing an interface, know how to do it • Can also include public named constants • Acts as a new type • You can define object reference variables using interface names SYNTAX public interface Interface_Name { Public_Named_Constant_Definitions . . . Public_Method_Heading_1; . . . Public_Method_Heading_n; }

  5. /** • An interface of static methods to convert measurements • between feet and inches. • */ • public interface Convertible • { • public static final int INCHES_PER_FOOT = 12; • public static double convertToInches(double feet); • public static double convertToFeet(double inches); • }

  6. Making Use of Interfaces • A class implementing an interface needs to define each of its methods • Interfaces help designers and programmers • Several classes can implement the same interface • A variable’s type determines what method names can be used, but the object the variable references determines which definition of the method will be used • Available method names are determined by the compiler • Called method is determined at run time by late binding • Dynamic binding also applies to interfaces public class Canary implements Pet Interfaces are a great tool for abstraction and top-down design

  7. Interfaces Can Be Extended Interfaces can inherit multiple classes public interface Nameable { public void setName(String petName); public String getName(); } public interface Callable extends Nameable { public void come(String petName); } • public interface Capable • { • public void hear(); • public String respond(); • } • public interface Trainable extends Callable, Capable • { • public void sit(); • public String speak(); • public void lieDown(); • }

  8. Abstract Classes • Sometimes we design classed to be used as base classes • We do not intend for these classes to actually be instantiated • We do not provide implementations for all of their methods, constructors, etc. • Abstract classes serve exactly this purpose • public abstract class ShapeBase • { • … public abstract void draw(); public StringgetShapeName(); • } Abstract classes can never be instantiated

More Related