Once an interface has been defined, one or more classes can implement that interface.
To implement an interface, include the implements clause in a class definition, and then create the methods declared by the interface.
The general form of a class that includes the implements clause looks like this:
Access-specifier class classname [extends superclass] [implements interface, [, interface..]]
{
// class body}
If a class implements from more than one interface, names are separated by comma.
If a class implements two interfaces that declare the same method, then the same method will be used by clients of either interface.
The methods that implement an interface must be declared as public.
The type-signature of implementing method must match exactly the type signature specified in the interface. interface religion
{
String city = new String("Amritsar");
void greet();
void pray();
}
class gs implements religion
{
public void greet()
{
System.out.println("We greet - ABCD");
}
public void pray()
{
System.out.println("We pray at " + city + " XYZ ");
}
}
class iface1
{
public static void main(String args[])
{
gs sikh = new gs();
sikh.greet();
sikh.pray();
}
}
Output :
We greet - ABCD
We pray at Amritsar XYZ
EX 2 :
interface i1
{
void dispi1();
}
interface i2
{
void dispi2();
}
class c1 implements i1
{
public void dispi1()
{
System.out.println("This is display of i1");
}
}
class c2 implements i2
{
public void dispi2()
{
System.out.println("This is display of i2");
}
}
class c3 implements i1, i2
{
public void dispi1()
{
System.out.println("This is display of i1");
}
public void dispi2()
{
System.out.println("This is display of i2");
}
}
class iface2
{
public static void main(String args[])
{
c1 c1obj = new c1();
c2 c2obj = new c2();
c3 c3obj = new c3();
c1obj.dispi1();
c2obj.dispi2();
c3obj.dispi1();
c3obj.dispi2();
}
}
Output :
This is display of i1
This is display of i2
This is display of i1
This is display of i2
EX 3 : // Implementing interface having common function name
interface i1
{
void disp();
}
interface i2
{
void disp();
}
class c implements i1, i2
{
public void disp()
{
System.out.println("This is display .. ");
}
}
class iface7
{
public static void main(String args[])
{
c cobj = new c();
cobj.disp();
}
}
Output :
This is display ..
Note : When implementing an interface method, it must be declared as public. It is possible for classes that implement interfaces to define additional members of their own.Partial Implementation of Interface :
If we want to implement an interface in a class we have to implement all the methods defined in the interface.
But if a class implements an interface but does not fully implement the method defined by that interface, then that class must be declared as abstract.
EX : interface i1
{
void disp1();
void disp2();
}
abstract class c1 implements i1
{
public void disp1()
{
System.out.println("This is display of 1");
}
}
class c2 extends c1
{
public void disp2()
{
System.out.println("This is display of 2");
}
}
class iface
{
public static void main(String args[])
{
c2 c2obj = new c2();
c2obj.disp1();
c2obj.disp2();
}
}
Output :
This is display of 1
This is display of 2
0 comments:
Post a Comment