INTERFACE IN JAVA
-Compiled by Nikahat Mulla
Agenda
▪ Single vs Multiple Inheritance
▪ Java Interface
▪ Interface Syntax
▪ Implementing interfaces
2
Single vs. Multiple Inheritance
• Some object-oriented languages allow multiple inheritance,
which allows a class to be derived from two or more classes,
inheriting the members of all parents
• The price: collisions, such as the same variable name, same
method name in two parents, have to be resolved
• Java decision: single inheritance, meaning that a derived class
can have only one parent class
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
4
Interface: Syntax
interface is a reserved word
public interface Doable
{
public static final String NAME=“XYZ”;
public void doThis();
public int doThat();
public void doThis2 (float value, char ch);
public boolean doTheOther (int num);
}
A semicolon immediately
follows each method header
No method in an
interface has a definition (body) 5
Interfaces (more)
✓ An interface is treated like a special class in Java. Each interface is
compiled into a separate bytecode file, just like a regular class
✓ Can not create instance from an interface using the new operator
✓ Can be used as a data type for a reference variable
✓ Relationship between the class and the interface is known as interface
inheritance
✓ A constant defined in an interface can be accessed using the syntax
InterfaceName. CONSTANT_NAME
✓ All data fields are public final static and all methods are public abstract in
an interface, Java allows these modifiers to be omitted.
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
7
Implementing Interfaces
public class Something implements Doable
{
public void doThis ()
{ implements is a
// whatever reserved word
}
public void doThat ()
{ Each method listed
// whatever in Doable is
}
given a definition
// etc.
}
public class ManyThings implements Doable, AnotherDoable 8
Interfaces: Examples from
Java Standard Class Library
• The Java Standard Class library defines many interfaces:
• the Iterator interface contains methods that allow the user
to move through a collection of objects easily
• hasNext(), next(), remove()
• the Comparable interface contains an abstract method called
compareTo, which is used to compare two objects
if(obj1.compareTo(obj2)< 0)
System.out.println(“obj1 is less than obj2”);
9
Polymorphism via Interfaces
• Define a polymorphism reference through interface
• declare a reference variable of an interface type
Doable obj;
• the obj reference can be used to point to any object of
any class that implements the Doable interface
• the version of doThis depends on the type of object that
obj is referring to:
obj.doThis();
10
Example: Polymorphism via Interface
• Consider a SmartPhone class which extends
the Phone class and implements MusicPlayer
and Camera interfaces
Refer InterfaceExample.java
11
More Examples
Speaker guest; public interface Speaker
{
guest = new Philosopher(); public void speak();
guest.speak(); }
guest = Dog(); class Philosopher extends Human
guest.speak(); implements Speaker
{
//
Speaker special; public void speak()
special = new Philosopher(); {…}
public void pontificate()
special.pontificate(); // compiler error {…}
}
class Dog extends Animal
implements Speaker
Speaker special; {
special = new Philosopher(); //
public void speak()
((Philosopher)special).pontificate(); {
…
}
} 12
Extending Interfaces
✓ An interface can inherit other interfaces using the extends
keyword (here multiple interfaces are allowed with extends
unlike classes)
public interface NewInterface extends Interface1, ...,
InterfaceN {
// constants and abstract methods
}
✓ A class implementing NewInterface must implement the
abstract methods defined in NewInterface, Interface1, …. and
InterfaceN
✓ An interface can extend other interfaces but not classes
✓ A class can extend its superclass and implement multiple
interfaces.
13
Interface Summary
• In Java, an interface is a reference type, similar to a class
• Can contain constants & method signatures
• There are no method bodies
• Cannot be instantiated — they can only be implemented by classes or
extended by other interfaces
• Variables & methods declared in interfaces are public by default
• An interface can extend another interface
• A class can implement multiple interfaces
14
Interface Summary (Contd…)
Why Interface?
• To reveal an object's programming interface
(functionality of the object) without revealing
its implementation
✓ This is the concept of encapsulation
✓ The implementation can change without affecting the
caller of the interface
✓ The caller does not need the implementation at the
compile time
✓ It needs only the interface at the compile time
✓ During runtime, actual object instance is associated
with the interface type
15
Interface Summary (Contd…)
Why Interface?
• To have unrelated classes implement similar
methods (behaviors)
• Example:
✓ Class Line and class MyInteger
✓ They are not related through inheritance
✓ You want both to implement comparison methods
- checkIsGreater(Object x, Object y)
- checkIsLess(Object x, Object y)
- checkIsEqual(Object x, Object y)
✓ Define Comparison interface which has the three abstract
methods above
16
Interface Summary (Contd…)
Why Interface?
• To model multiple inheritance
✓A class can implement multiple interfaces
while it can extend only one class
17
Interface Summary (Contd…)
• Example of an interface declaration:
public interface Interface1
{
public void set(int i);
public int get();
}
Interface1 i1 = new Interface1();
Interfaces and Abstract
classes can not be
instantiated
18
Thank You