80 likes | 178 Views
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
E N D
Intro to CS – Honors IInterfaces and Abstract Classes Georgios Portokalidis gportoka@stevens.edu
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
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!");
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; }
/** • 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); • }
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
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(); • }
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