2. Interfaces
Another way to achieve abstraction in Java, is with
interfaces.
An interface is a completely "abstract class" that is used
to group related methods with empty bodies:
2
3. Java Interface
A Java interface is a collection of constants and abstract methods
abstract method: a method header without a method body; we declare an
abstract method using the modifier abstract
since all methods in an interface are abstract, the abstract modifier is
usually left off
Methods in an interface have public visibility by default
3
5. To access the interface methods, the interface must be
"implemented" (like inherited) by another class with
the implements keyword (instead of extends).
The body of the interface method is provided by the
"implement"
5
6. Implementing an Interface
A class formally implements an interface by
stating so in the class header in the implements clause
a class can implement multiple interfaces: the interfaces are listed in the
implements clause, separated by commas
If a class asserts that it implements an interface, it must define all
methods in the interface or the compiler will produce errors
6
7. Implementing Interfaces
7
public class Something implements Doable
{
public void doThis ()
{
// whatever
}
public void doThat ()
{
// whatever
}
// etc.
}
implements is a
reserved word
Each method listed
in Doable is
given a definition
public class ManyThings implements Doable, AnotherDoable
8. Interfaces: An Example
A class that implements an interface can implement other methods as
well
8
9. Interface face1
{
void meth1(int a, int b);
}
Interface face2 extends face1
{
void meth2(int a);
}
Interface face3 extends face2
{
void meth3();
}
Class MyClass implements face3
{
public void meth1(int a, int b)
{System.out.println(“This is method 1 from face 1”);}
public void meth2(int a)
{System.out.println(“This is method 2 from face 2”)}
public void meth3()
{System.out.println(“This is method 3 from face 3”)}
}
}
9
11. // Interface
interface Animal {
public void animalSound(); // interface method (does not have a body)
public void sleep(); // interface method (does not have a body)
}
// Cat "implements" the Animal interface
class Cat implements Animal {
public void animalSound() {
// The body of animalSound() is provided here
System.out.println("The cat says: Mew Mew");
}
public void sleep() {
// The body of sleep() is provided here
System.out.println("Zzz");
}
} 11
12. class Main {
public static void main(String[] args) {
Cat myCat = new Cat(); // Create a Cat object
myCat.animalSound();
myCat.sleep();
}
}
12
13. Notes on Interfaces:
Like abstract classes, interfaces cannot be used to
create objects (in the example above, it is not
possible to create an "Animal" object in the
MyMainClass)
Interface methods do not have a body - the body is
provided by the "implement" class
On implementation of an interface, you must
override all of its methods
Interface methods are by default abstract and public
Interface attributes are by
default public, static and final
An interface cannot contain a constructor (as it
cannot be used to create objects)
13
14. Why And When To Use
Interfaces?
1) To achieve security - hide certain details and only show
the important details of an object (interface).
2) Java does not support "multiple inheritance" (a class can
only inherit from one superclass). However, it can be
achieved with interfaces, because the class
can implement multiple interfaces.
Note: To implement multiple interfaces, separate them
with a comma
14