Interfaces - PowerPoint PPT Presentation

About This Presentation
Title:

Interfaces

Description:

Using interfaces, Java supports somehow a weak form of multiple inheritance (it ... If they are different signature, they are considered to be overloaded. ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 15
Provided by: IBMU362
Learn more at: http://www.cs.ucf.edu
Category:

less

Transcript and Presenter's Notes

Title: Interfaces


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

2
Interfaces
  • interface I1 Although we do not write here, it
    is assumed
  • that CONST1 is declared as a constant (with
  • int CONST15 keywords public, final and
    static)
  • void m1() Although we do not write here, it
    is assumed
  • that m1 is declared with keywords public
  • and abstract.

3
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
  • class classname implements interfacename
  • .
  • . implementations of all methods in the
    interface must be provided here.

4
Implementing Interfaces - Example
  • class C1 implements I1
  • .
  • . implementations of all methods in the
    interface I1
  • . must be provided here.
  • .
  • All constants in I1 can be accessible in C1
    as
  • its own variables.

5
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 I1
  • void m1()
  • class C1 implements I1
  • public void m1() System.out.println(Implement
    ation in C1)
  • class C2 implements I1
  • public void m1() System.out.println(Implement
    ation in C2)

6
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.
  • C1 C1 C2 C3
  • C2 C4
  • single inheritance multiple inheritance
  • Java supports only single inheritance among
    classes. (C support multiple inheritance.).
    Multiple inheritance is difficult to use.

7
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).
  • class classname implements interface1,,interface
    n
  • .
  • .
  • . implementations of all methods in the all
    interfaces
  • must be provided here.

8
Implementing More Than One Interface
  • interface I1
  • void m1()
  • interface I2
  • void m2() C must implement all methods in I1
    and I2.
  • void m3()
  • class C implements I1, I2
  • public void m1() System.out.println(C-m1)
  • public void m2() System.out.println(C-m2)
  • public void m3() System.out.println(C-m3)

9
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.

10
Resolving Name Conflicts Among Interfaces
  • interface I1
  • void m1()
  • void m2()
  • void m3()
  • interface I2
  • void m1(int a) There will be a compilation
    error for m3.
  • void m2()
  • int m3()
  • class C implements I1, I2
  • public void m1() // implementation of
    m1 in I1
  • public void m1(int x) // implementation
    of m1 in I2
  • public void m2() // implementation of
    m1 in I1 and I2

11
Inheritance Relation Among Interfaces
  • Similarly for classes, interfaces can hold an
    inheritance relation among them.
  • interface I2 extends I1
  • Now, I2 contains all abstract methods of I1 plus
    its own abstract methods.
  • The classes implementing I2 must implement all
    methods in I1 and I2.

12
Interfaces as Data Types
  • Just as classes are data types, so are
    interfaces.
  • Different from classes We cannot create an
    instance of an interface.
  • interface I1
  • class C1 implements I1
  • class C2 extends C1
  • // a variable can be declared as type I1
  • I1 x
  • A variable declared as I1, can store objects of
    C1 and C2.

13
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 C1 extends class C2, then C1 is a
    subtype of C2.
  • If interface I1 extends interface I2, then I1 is
    a subtype of I2.
  • 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 T1 is a subtype of T2, then T1 is a
    subtype of T2.

14
Interface Example
  • public interface Juggleable
  • void tossIt()
  • void catchIt()
  • public class Fruit implements Juggleable
  • public void tossIt() ...
  • public void catchIt() ...
  • public class Apple extends Fruit ...
  • public class Orange extends Fruit ...
  • public class Knife implements Juggleable ...
  • Juggleable thingsToJuggle new Juggleable3
  • thingsToJuggle0 new Apple()
  • thingsToJuggle1 new Orange()
  • thingsToJuggle2 new Knife()
Write a Comment
User Comments (0)
About PowerShow.com