Interfaces In Java interfaces declare methods but they

Interfaces • In Java, interfaces declare methods but they do not provide any implementation. • A Java interface is a collection of abstract methods and constants. interfacename { - constant declarations - abstract methods } • An interface looks like a class, but it is not a class. It is not in the class hierarchy. Fall 2002 COP 3330 Object Oriented Programming 1

Interfaces interface I 1 { int CONST 1=5; void m 1() ; } Fall 2002 Although we do not write here, it is assumed that CONST 1 is declared as a constant (with keywords public, final and static) Although we do not write here, it is assumed that m 1 is declared with keywords public and abstract. COP 3330 Object Oriented Programming 2

Implementing Interfaces • Interfaces are intended to capture the common characteristics and behavior of the classes that implement the interfaces. • A class that implements an interface must provide implementations for all of the methods in that interface. • A class can implement an interface as follows: classname implements interfacename {. . implementations of all methods in the interface } must be provided here. Fall 2002 COP 3330 Object Oriented Programming 3

Implementing Interfaces - Example class C 1 implements I 1 {. . implementations of all methods in the interface I 1. must be provided here. . } All constants in I 1 can be accessible in C 1 as its own variables. Fall 2002 COP 3330 Object Oriented Programming 4

Implementing Interfaces (cont. ) • An interface can be implemented by multiple classes. • Each implementing class can provide their own unique versions of the method definitions. interface I 1 { void m 1() ; } class C 1 implements I 1 { public void m 1() { System. out. println(“Implementation in C 1”); } } class C 2 implements I 1 { public void m 1() { System. out. println(“Implementation in C 2”); } } Fall 2002 COP 3330 Object Oriented Programming 5

Single Inheritance versus Multiple Inheritance • Single inheritance means that each subclass has exactly one superclass, and that subclass can only inherit from its single superclass. • Multiple inheritance means that each subclass can have more than one superclass, and that subclass can inherit from all its superclasses. C 1 C 2 single inheritance C 1 C 2 C 3 C 4 multiple inheritance • Java supports only single inheritance among classes. (C++ support multiple inheritance. ). Multiple inheritance is difficult to use. Fall 2002 COP 3330 Object Oriented Programming 6

Weak Form of Multiple Inheritance (!) • Using interfaces, Java supports somehow a weak form of multiple inheritance (it is not actual multiple inheritance). • In Java, a class may implement more than one interface, and this can be seen as a weak form of multiple inheritance (but it is not multiple inheritance). classname implements interface 1, …, interfacen {. . . implementations of all methods in the all interfaces } must be provided here. Fall 2002 COP 3330 Object Oriented Programming 7

Implementing More Than One Interface interface I 1 { void m 1(); } interface I 2 { void m 2() ; void m 3() ; } class C implements public void m 1() public void m 2() public void m 3() } Fall 2002 C must implement all methods in I 1 and I 2. I 1, I 2 { { System. out. println(“C-m 1”); } { System. out. println(“C-m 2”); } { System. out. println(“C-m 3”); } COP 3330 Object Oriented Programming 8

Resolving Name Conflicts Among Interfaces • Since a class may implement more than one interface, the names in those interfaces may collide. • To solve name collisions, Java use a simple mechanism. • Two methods that have the same name will be treated as follows in Java: – If they are different signature, they are considered to be overloaded. – If they have the same signature and the same return type, they are considered to be the same method and they collapse into one. – If they have the same signature and the different return types, a compilation error will occur. Fall 2002 COP 3330 Object Oriented Programming 9

Resolving Name Conflicts Among Interfaces interface I 1 { void m 1(); void m 2(); void m 3(); } interface I 2 { void m 1(int a); There will be a compilation error for m 3. void m 2(); int m 3(); } class C implements I 1, I 2 { public void m 1() { … } // implementation of m 1 in I 1 public void m 1(int x) { … } // implementation of m 1 in I 2 public void m 2() { … } // implementation of m 1 in I 1 and I 2 } Fall 2002 COP 3330 Object Oriented Programming 10

Inheritance Relation Among Interfaces • Similarly for classes, interfaces can hold an inheritance relation among them. interface I 2 extends I 1 { … } • Now, I 2 contains all abstract methods of I 1 plus its own abstract methods. • The classes implementing I 2 must implement all methods in I 1 and I 2. Fall 2002 COP 3330 Object Oriented Programming 11

Interfaces as Data Types • Just as classes are data types, so are interfaces. • Different from classes: We cannot create an instance of an interface I 1 { … } class C 1 implements I 1 { … } class C 2 extends C 1 { … } // a variable can be declared as type I 1 x; • A variable declared as I 1, can store objects of C 1 and C 2. Fall 2002 COP 3330 Object Oriented Programming 12

Subtypes • Each interface also defines a data type. • The interface relation among interfaces, and the implementation relation between a class and interfaces create subtype relations. • Complete subtype relations in Java as follows: – If class C 1 extends class C 2, then C 1 is a subtype of C 2. – If interface I 1 extends interface I 2, then I 1 is a subtype of I 2. – If class C implements interface I, then C is a subtype of I. – For every interface I, I is a subtype of Object. – For every type T (reference or primitive), T[] (array type of T) is a subtype of Object. – If type T 1 is a subtype of T 2, then T 1[] is a subtype of T 2[]. Fall 2002 COP 3330 Object Oriented Programming 13

Interface Example public interface Juggleable { void toss. It(); void catch. It(); } public class Fruit implements Juggleable { public void toss. It() {. . . } public void catch. It() {. . . } } public class Apple extends Fruit {. . . } public class Orange extends Fruit {. . . } public class Knife implements Juggleable {. . . } Juggleable[] things. To. Juggle = new Juggleable[3]; things. To. Juggle[0] = new Apple(); things. To. Juggle[1] = new Orange(); things. To. Juggle[2] = new Knife(); Fall 2002 COP 3330 Object Oriented Programming 14
- Slides: 14