You are on page 1of 6

Interfaces

1. Interface is also one of the type of class it contains only abstract methods.
2. For the interfaces also .class files will be generated.
3. Each and every interface by default abstract hence it is not possible to create an object.
4. Interfaces not alternative for abstract class it is extension for abstract classes.
5. 100 % pure abstract class is called interface.
6. The Interface contains only abstract methods means unimplemented methods.
7. Interfaces giving the information about the functionalities it are not giving the information
about internal implementation.
8. To provide implementation for abstract methods we have to take separate class that class we
can called it as implementation class for that interface.
9. Interface can be implemented by using implements keyword.
10. For the interfaces also the inheritance concept is applicable.

Syntax:-
Interface interface-name
Ex:- interface it1
Note: -
if we are declaring or not By default interface methods are public abstract

Interface it1 abstract interface it1


{ {
Void m1(); Both are same public abstract void m1();
Void m2(); public abstract void m2();
Void m3(); public abstract void m3();
} }

Ex 1:-
Interface it1
{
Void m1();
Void m2();
Void m3();
}
Class Test implements it1
{
Public void m1()
{
System.out.println(“m1-method implementation ”);
}
Public void m2()
{
System.out.println(“m2-method implementation”);
}
Public void m3()
{
System.out.println(“m3 –method implementation”);

106 | P a g e
}
Public static void main(String[] args)
{
Test t=new Test();
t.m1();
t.m2();
t.m3();
}
}
Ex 2:-
Interface it1
{
Void m1();
Void m2();
Void m3();
}
Abstract Class ImplClass implements it1
{
Public void m1() partially implemented class
{ so we have to declare abstract
System.out.println(“m1-method implementation ”);
}
}
Class Test extends ImplClass
{
Public void m2()
{
System.out.println (“m2-method implementation”);
} fully implemented class
Public void m3() so we have to create
{ obj then we can access.
System.out.println (“m3 –method implementation”);
}
Public static void main(String[] args)
{
Test t=new Test();
t.m1();
t.m2();
t.m3();
}

Note:-

107 | P a g e
By inheritance in the Test contains abstract methods like m2() & m3() hence we have to declare
that class as a abstract modifier.

We can take any number of classes but finally we have to provide implementation for the each
and every method of interface.

Note :- inheritance concept is also applicable for interfaces .

Ex:-
Without inheritance With inheritance
Interface it1 interface it1
{ {
Void m1(); Contains 3 methods void m1();
Void m2(); void m2(); Contains 3
Void m3(); void m3(); Methods
} }
Interface it2 interface it2 extends it2
{ {
Void m4(); contains 2 methods void m4(); Contains 5
Void m4(); void m5(); Methods
} }

Ex1:-
Interface it1
{
Void m1();
Void m2();
Void m3();
}
Interface it2 extends it1
{
Void m4();
Void m5();
}

Class Test implements it1 class Test implements it2 class Test implements it1,it2
{ { {
Here we have to provide here we have to provide here we have to provide
Three methods five methods five methods
Implementation implementation implementation
m1 & m2 & m3 m1 & m2 & m3& m4 & m5 m1 & m2 & m3 & m4 & m5
} } }

108 | P a g e
Nested interfaces:-
Ex:-an interface can be declare inside the class is called nested interface.
class Test1
{
interface it1
{
void add();
}
};
class Test2 implements Test1.it1
{
public void add()
{
System.out.println("add method");
}
public static void main(String[] args)
{
Test2 t=new Test2();
t.add();
}
};
Ex:-an interface can be declare inside the another interface is called nested interface.
interface it2
{
interface it1
{
void m1();
}
};
class Test2 implements it2.it1
{
public void m1()
{
System.out.println("m1 method");
}
public static void main(String[] args)
{
Test2 t=new Test2();
t.m1();
}
};
Note:-

class extends class


class implements interface
interface extends interface

109 | P a g e
class A extends B======good
class A extends B,C====bad
class A implements it1=====good
class A implements it1,it2,it3====good

interface it1 extends it2-----good


interface it1 extends it2,it3---good
interface it1 extends A---------bad

class A extends B implements it1,i2,it3=====good


class A implements it1 extends B=====bad

Adaptor class:-
It is a intermefdiate class between the interface and user defined class. And it contains empty
implementation of interface methods.

Limitation of interface advantage of adaptor classes

interface it interface it
{ {
void m1(); void m1();
void m2(); void m2();
; ;
; ;
void m100() void m100()
} }
Class Test implements it class Adaptor implements it
{ {
Must provide implementation of 100 methods void m1(){}
otherwise compiler raise compilation error void m2(){}
} ;
;
void m100(){}
};
class Test implements it
{
must provide the 100 methods implementation
};
class Test extends Adaptor
{
provide the implementation of required
methods.
};

110 | P a g e
Ex:-
interface it1
{
void m1();
void m2();
}
class X implements it1
{
public void m1(){}
public void m2(){}
};
class Test1 implements it1
{
public void m1()
{
System.out.println("m1 method");
} class Test2 extends X
public void m2() {
{ public void m1()
System.out.println("m2 method"); {
} System.out.println("adaptor m1
public static void main(String[] args) method");
{ }
Test1 t=new Test1(); public static void main(String... arhs)
t.m1(); {
t.m2(); Test2 t=new Test2();
} t.m1();
}; }};

111 | P a g e

You might also like