Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interfaces besides classes, Java recognizes another type, an interface interface is used to completely shield off all implementation from the programmer.

Similar presentations


Presentation on theme: "Interfaces besides classes, Java recognizes another type, an interface interface is used to completely shield off all implementation from the programmer."— Presentation transcript:

1 Interfaces besides classes, Java recognizes another type, an interface interface is used to completely shield off all implementation from the programmer of client classes interfaces don't contain any code other than that, an interface is similar to a class – important distinction: methods are declared without bodies programming with interfaces is becoming more popular – rightly so

2 Interface Declaration an interface if declared like a class – only interface replaces class, e.g. interface Arithmetic { // field declarations // method declarations } only signatures of methods – only modifier, return type, name and parameter list – no body, only ; – e.g. interface Arithmetic { public Arithmetic add (Arithmetic number); public Arithmetic subtract (Arithmetic number); } fields are typically used only for constants

3 Interfaces and Classes interfaces are useless without classes – someone must implement their methods – interfaces cannot be instantiated a class can implement an interface – it must supply all the methods defined in the interface – e.g. class Complex implements Arithmetic { public Arithmetic add (Arithmetic number) {…} public Arithmetic subtract (Arithmetic number) {…} } – several classes typically implement an interface interface is a contract between a set of classes and their clients the class must fulfill the contract defined in the interface – i.e. implement all the methods defined in the interface

4 Classes Implementing Interfaces a class can implement several interfaces – it must supply all the methods defined in all the interfaces class Complex implements Arithmetic, Prompting { public Arithmetic add (Arithmetic number) {…} public void promptFor (String prompt) {…} } it can use all fields declared in the interfaces it implements it can have other fields and methods

5 Interface Variables a variable can be declared using an interface type but it's value is always an object of an implementing class – e.g. Arithmetic number = new Complex (); only methods defined in the interface can be called on it e.g. if Complex defines vector() and Arithmetic doesn't then calling number.vector(); is illegal way out: casting – convert interface variable into the class type and before the call e.g. ((Complex) number).vector(); – this is frowned upon – its dangerous: compiler can't check whether types correspond possible run-time error, if the actual value isn't the class casted

6 Interface Variables (cont.) a variable declared using an interface type can be assigned – various objects of any classes implementing the interface – any variable of the same interface e.g. suppose Complex and Fraction both implement Arithmetic then the following code is ok: Arithmetic number1 = new Complex (); Arithmetic number2 = number1; number2 = new Fraction (); Arithmetic number3 = number2.add(number1); but who knows what happens when we add a Complex number to a Fraction !


Download ppt "Interfaces besides classes, Java recognizes another type, an interface interface is used to completely shield off all implementation from the programmer."

Similar presentations


Ads by Google