Download presentation
Presentation is loading. Please wait.
Published byBrittney Adams Modified over 9 years ago
1
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Interface COMP 112 2014T1 14
2
COMP 112 14: 2 © David Streader Overview Interface as 1.Object with only empty method bodies. 2.Signature or Type 3.Contract Interface used to 1.Decouple software components 2.Hide internal detail 3.Provide data abstraction 4.Provide a kind of multiple inheritance
3
COMP 112 14: 3 © David Streader Interface and Signature All you know to call a method is its signature name [ ] +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
COMP 112 14: 4 © David Streader Refresh Java is an Object Oriented Language Objects are collections of Fields and attached methods When you pass an Object as a parameter you: 1.Pass the values held in the fields 2.Pass the methods attached to the object What design options are opened up by passing methods?
5
COMP 112 14: 5 © David Streader Holes in code! The Person Object has method greater() The Sorted method has an array of Person s as parameter! Sorted is also parameterised on method greater() Hole in code Plug for hole All we need know is its signature
6
COMP 112 14: 6 © David Streader 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 (L4 equals, L11 reading objects from files) you can: 1.check which type of object you have by using instanceof for example if (s instanceof Student) {… 2. cast an object to fix its type for example Student s = (Student) p; Both techniques become more useful when you are using interfaces
7
COMP 112 14: 7 © David Streader A Student is a Person Person as an interface Student implements a Person The sorted method will accept a Student parameter. If Lecturer implements person it too can be used.
8
COMP 112 14: 8 © David Streader 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).
9
COMP 112 14: 9 © David Streader Interfaces and Design Decouple different Objects! Add flexibility without recompilation! DodgemCar Example: 1.Allow new walls and tubbys to be added as game progresses. 2.Allow new type of obstacles (obstacles that respond differently when collided with) to be added as game progresses
10
COMP 112 14: 10 © David Streader Design of Dodgens Game Flexibility with out recompilation Add an ArrayList of the 4 Walls refactor code to be able to collide with any wall in list. We want to add the central tubby to the list Refactor to an ArrayList of Obstacles. Make Wall and Tubby implement the Obstacle interface With this design: 1. New walls and tubbys can be added at runtime 2.New Objects that implement the Obstacle interface can be added.
11
COMP 112 14: 11 © David Streader 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
COMP 112 14: 12 © David Streader 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
COMP 112 14: 13 © David Streader Example Scanner implement an iterator interface. From an Array list you can construct an iterator While code could be applied to an ArrayList
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.