You are on page 1of 14

Interfaces

SEED Infotech Pvt. Ltd. 1
Objectives of this session

Identify the need for interfaces .
Demonstrate Interfaces in detail using the
Printable interface
Distinguish between interfaces & abstract classes
Distinguish between implements & extends
keywords

SEED Infotech Pvt. Ltd. 2
Implementation inheritance

Where the sub class inherits all the code available in the
super class.
Useful for reusability of code.
Problem arises when multiple inheritance has to be achieved .
The OO language like C++ supports multiple inheritance at
the cost of added complexity and ambiguity at times.
Java does not support multiple inheritance.
Java supports a single chain of implementation inheritance.

SEED Infotech Pvt. Ltd. 3
Interfaces – Their need

To overcome the lack of multiple inheritance Java uses
INTERFACE INHERITANCE.
Interface is essentially a collection of constants & abstract
methods.
The interface approach is sometimes known as
programming by contract.
An interface is used via the keyword "implements". Thus a
class can be declared as
class MyClass implements Sun, Fun{
...
}

SEED Infotech Pvt. Ltd. 4
Interfaces

Interfaces are used for -
– Expanding the scope for polymorphism.
– Achieving multiple inheritance as interface
adds to the type of the class.
– Implementing callback methods.

SEED Infotech Pvt. Ltd. 5
Interfaces

A Java interface definition looks like a class definition that
has only abstract methods, although the abstract keyword
need not appear in the definition.

public interface Testable
{
void method1();
void method2(int i, String s);
}

SEED Infotech Pvt. Ltd. 6
Interfaces - rules

Methods in an interface are always public &
abstract.
Data members in a interface are always public,
static & final.
A sub class can only have a single superclass in
java.
But a class can implement any number of
interfaces.

SEED Infotech Pvt. Ltd. 7
Interface

The valid combinations are

class extends class
class implements interface
interface extends interface

All other combinations are invalid.

SEED Infotech Pvt. Ltd. 8
Implementing interfaces - the Printable Ex

public interface Printable
{
void print();
}

SEED Infotech Pvt. Ltd. 9
Printable Example

class Emp implements Printable
{ // instance variables……..
public Emp(int id, String nm, date d){…..}
public void print()
{
System.out.println("employee is" + this);
}
public String toString() {
return(empid + "\n" + name + "\n" + birthdate );
}

SEED Infotech Pvt. Ltd. 10
Printable Example

public static void main(String args[]) {
Printable eArr[] = new Printable[5];
eArr[0] =new Emp(…);
eArr[1] =new Emp(…);
…...
eArr[4] = new Emp(…);
Util.PrintAll (eArr);
}
}

SEED Infotech Pvt. Ltd. 11
Printable Example

class Util
{
public static void PrintAll( Printable objArr[] )
{
for(int i = 0; i < objArr.length;i++)
{
objArr[ i ] . print();
}
}

SEED Infotech Pvt. Ltd. 12
Interfaces & Abstract classes

Abstract classes are used Interfaces can be
only when there is a “is-a” implemented by classes
type of relationship that are not related to one
between the classes. another.
You cannot extend more You can implement more
than one abstract class. than one interface.
Abstract class can contain Interfaces contain only
abstract as well as abstract methods.
implemented methods

SEED Infotech Pvt. Ltd. 13
Interfaces

Cloneable
Tagging interface:used to declare that the class
objects can be cloned.

SEED Infotech Pvt. Ltd. 14