Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interface Java 7 COMP 112 2017T1.

Similar presentations


Presentation on theme: "Interface Java 7 COMP 112 2017T1."— Presentation transcript:

1 Interface Java 7 COMP T1

2 Overview Interface as Object with only empty method bodies.
Signature or Type Contract Interface used to Decouple software components Hide internal detail Provide data abstraction Provide a kind of multiple inheritance

3 Interface and Signature
All you know to call a method is its signature <return type> name [<parameter type>] exceptions The signature of an Object is the set of its method signatures To use an object know its signature. An Interface is a signature Implementing an interface means implementing the methods. One Object can implement several interfaces Several Objects can implement the same interface

4 Refresh Java is an Object Oriented Language
Objects are collections of Fields and attached methods When you pass an Object as a parameter you: Pass the values held in the fields Pass the methods attached to the object What design options are opened up by passing methods? Dynamic (Runtime) dispatch. This means what code is actually run is decided while a program is running!

5 Holes in code! Hole The Person Interface has method greater()
The Sorted method has an array of Persons as parameter! What shape is the hole? Code decided at run time Hole in code All we need know is its signature

6 Decomposing Java projects
Java has a predefined Comparator interface. Many Java sort functions have a Comparator shaped hole that can be plugged by passing a Comparator as a parameter This means Oracle write the sort program not knowing anything about you or your code. You write the comparator and now your code will work with code written by a remote group. When working as part of a large team you will need to decompose a problem into parts connected via an interface. So that code developed by different team members has some chance of working together.

7 Colored Plugs Different objects implement the interface differently
Two objects implement an interface then either can be used when the interface is expected Different objects can be different types of Plug Recall (equals, reading objects from files) you can: check which type of object you have by using instanceof for example if (s instanceof Student) {… cast an object to fix its type for example Student s = (Student) p; Both techniques become more useful when you are using interfaces

8 A Student is a Person Person is an interface
Student implements a Person The sorted method will accept a Student[] parameter. If Lecturer implements person it too can be used.

9 Why Use Interfaces? Interfaces allow different parts of a large system to be implemented separately. For example: The java virtual machine JVM was developed independently of your software and publicized a MouseListener interface. When you implement this interface the JVM is able to call you back when the mouse is clicked. Interfaces allow new objects to be designed that provide new functionality (methods) with out recompiling the system (the code that calls the methods).

10 Interfaces and Design Flexibility with out recompilation
Add an ArrayList<Walls> of the 4 Walls refactor code to be able to collide with any wall in list. We want to add the central obstacle to the list Refactor to an ArrayList<Hittable> of things that can be hit. Make Wall and obstacle implement the Hittable interface With this design: New walls and obstacles can be added at runtime New objects that implement the Hittable interface can be added.

11 Summary An Interface is a Class without method bodies
A Class implements an interface if it implements all the methods Interfaces help decouple code. The Java Run Time System and your GUI code. After a system is up and running you can design new Objects with new functionality and the system can use this new functionality with out being recompiled. Sorting Lectures based on age.

12 Empty methods and Contracts
An Object without Fields and with empty method bodies is an interface An interface defines a weak sort of contract. An object that implements the interface can be used where ever interface was expected. What the object does is not guaranteed

13 Example Scanner implement an iterator interface. While code could be
From an ArrayList you can construct an iterator While code could be applied to an ArrayList


Download ppt "Interface Java 7 COMP 112 2017T1."

Similar presentations


Ads by Google