1 / 25

Interfaces

Interfaces. Interfaces.

bunme
Download Presentation

Interfaces

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

  2. Interfaces • An interface in the Java programming language is an abstract type that is used to specify an interface (in the generic sense of the term) that classes must implement. Interfaces are declared using the interface keyword and may only contain method signatures and constant declarations (variable declarations which are declared to be both static and final).

  3. states that this is an interface, not a class note no bodies for the methods Java Interfaces • a java interface is just a collection of abstract methods (i.e. we state the signatures, but not the bodies) public interface MyStack { public int size(); public boolean isEmpty(); public Object top(); public void push(Object elt); public Object pop(); }

  4. Continued • As interfaces are implicitly abstract, they cannot be directly instantiated. Object references in Java may be specified to be of an interface type; in which case they must either be null, or be bound to an object which implements the interface. • The keyword implements is used to declare that a given class implements an interface. A class which implements an interface must either implement all methods in the interface, or be an abstract class

  5. Advantages of Interfaces • Provide a standard set of methods for a group of classes. • This is Java’s implementation of an Abstract Data Type (ADT) • Objects of these classes can be accessed by the standard set of methods without considering about their location in class hierarchy. • Support selective multiple inheritance. • e.g.java.util.LinkedList

  6. Benefit • One benefit of using interfaces is that they simulate multiple inheritance. All classes in Java (other than java.lang.Object, the root class of the Java type system) must have exactly one base class; multiple inheritance of classes is not allowed. However, a Java class/interface may implement/extend any number of interfaces.

  7. Data Abstraction • if the interface remains the same, clients don't need to be changed, even if the implementation behind the interface changes public class Time { private int timeInSecs; //public methods } public class Time { private int hours; private int minutes private int secs; //same public methods //but with different //bodies }

  8. Defining an interface • Interfaces are defined with the following syntax • [visibility] interface InterfaceName [extends other interfaces] • { constant declarations • member type declarations • abstract method declarations }

  9. Abstract methods • The body of the interface contains abstract methods, but since all methods in an interface are, by definition, abstract, the abstract keyword is not required. Since the interface specifies a set of exposed behaviours, all methods are implicitly public.

  10. Example • public interface Predator { • boolean chasePrey(Prey p); • void eatPrey(Prey p); }

  11. Implements • Classes may implement an interface. For example

  12. public class Cat implements Predator { • public boolean chasePrey(Prey p) { // programming to chase prey p (specifically for a cat) } • public void eatPrey (Prey p) { // programming to eat prey p (specifically for a cat) } }

  13. Abstract • If a class implements an interface and does not implement all its methods, it must be marked as abstract. • If a class is abstract, one of its subclasses is expected to implement its unimplemented methods.

  14. Multiple Interfaces • Classes can implement multiple interfaces • public class Frog implements Predator, Prey { ... }

  15. Example of when Interfaces apply • Games Console • Interface required for Button Press • This is supplied by Device Driver • Need to do Different things in response to button press from Game to Game

  16. interfaces vs classes • a class definition can contain instance/class variables and instance/class methods, including both the signature and the body • a java interface contains only the signatures of public instance methods (and named constants) • a java interface acts like a specification • it says what it means to be e.g. a stack • to be a stack, an object must offer at least those methods in its public interface

  17. using Java interfaces • Java allows us to tell the compiler that a class will implement an interface • regard it as a contract stating that you will meet the specification • any class that implements an interface must provide implementations of the public methods (or it must be abstract, and its subclasses provide them) • the compiler will check, and if the bodies are not provided, it won't compile

  18. Summary • An interface is like an abstract class - it specifies what its methods should look like, but gives no body • if a class implements an interface, it is equivalent to signing a contract saying that it will provide a body for the specified method - Java will not compile the program unless we provide the method definition • we can refer to an object as though it were an object of the interface, and invoke the interface methods

  19. Java Interfaces • Identify a common set of methods for a group of classes implementing the interface • Shareconstants (static and final) between classes. • See examples.

  20. Interface Example - Shape public interface Shape { double PI = 3.1425926; //static and final constant. void draw(); void resize(); } public class Circle implements Shape { public void draw() { /* draw a circle*/ } public void resize() { /* draw a circle*/ } } public class Rectangle implements Shape { public void draw() { /* draw a circle*/ } public void resize() { /* draw a circle*/ } } implements

  21. Interface Example – Generator • Goal • Develop several implementations of an object, i.e. generator, that generate different sequence of integers, such as • constants - ConstantGenerator • random integers – RandomGenerator • primes – PrimeGenerator • InterfaceGenerator

  22. Simple Example • Interface • public interface IntExample1{ • public void sayHello(); } • Class which implements it • public class JavaInterfaceExample implements IntExample1{ • public void sayHello(){ • System.out.println("Hello Visitor !"); }

  23. Control Program • public static void main(String args[]){ JavaInterfaceExample JavaIExample = new JavaInterfaceExample(); • JavaIExample.sayHello(); }}

  24. Simple Example • Interface • public interface IntExample1{ • public void sayHello(); } • Class which implements it 2 • public class JavaInterfaceExample1 implements IntExample1{ • public void sayHello(){ • System.out.println("HI there Visitor !"); }

  25. Control Program • public static void main(String args[]){ • JavaInterfaceExample1 JavaIExample = new JavaInterfaceExample1(); • JavaIExample.sayHello(); }}

More Related