interface
An interface defines the contract between service provider and the client without highlighting internal implementation.
i.e Interface describes the services ,what service provider provides and what client get.
The main advantage of interface are :
1. we never high lite our implementation to the outside world,we can achieve security for our implementation.
2. With out effecting outside world,we can enhance our internal implementation. Interface is considered as 100% pure abstract class because we never keep implementation inside interface. And hence all the methods present inside an interface are abstract methods.
For the service provider point of view an interface defines the services provided by that person.
From the client point of view an interface describes what services he required.
Declaring an interface:
interface Interf
{
public void m1();
public void m2();
}
class Bea implements Interf
{
public void m1()
{
System.out.println(“implementation m1”);
}
public void m2()
{
System.out.println(“implementation m2”);
}
public static void main(String arg[])
{
Interf i=new Interf();
i.m1();
}
}
- If you want to provide implementation for any interface method ,it must be declared as public .
- The first concrete class which implements an interface must provide implementation for all the interface methods other wise the class must declared as abstract.
- We can declare an interface (top level) with <default> and public,abstract,strictfp modifiers.
All the interface methods by default abstract and public (either we are specify or not specify) not concrete method.
Examples:
Void m1();
public void m1();
abstract void m1();
public abstract void m1();
All the above are same .
Hence the above four methods declaration are identical.
An interface method never be declared as native ,strictfp,private ,static, synchronized and protected.
interface variables Declarations:
All the interface variable are by default public ,static and final.
Hence the following variable declaration inside an interface are identical.
interface interf
{
int x=10;
public int x=10;
public static final int x=10;
final int x=10;
static int x=0;
}
Hence an interface we never declared as private ,protected and transient.
All the interface variables must perform initialization at the time of declaration.
Hence the following code will not compile.
interface Interf
{
int x;
}
Inside implement classes we are not allowed to change the value of interface variable violation leads to compile time error.
Class Sample implement Interf
{
public static void main(String arg[])
{
x=20; //compile time error
}
}
A class can extend only one class at a time .But can interface can extend any number interfaces.
A class can implement any number of interfaces at a time.But interface never implement another interface.
Naming conflicts inside interface:
Case 1:
interface Left
{
public void m1();
}
interface Right
{
public void m1();
}
class Central implement Left,Right
{
public static void main(String arg[])
{
public void m1()
{
}
}
If two interfaces have the two are more same methods ,then only one method implementation is enough from both interfaces.
Case 2:
The interface having same method name but different arguments.
Interface Inter
{
public void m1();
public void m1(int x);
}
case 3:
Same method name,same arguments but different written types.
Interface Left
{
public void m1();
}
Interface Right
{
public int m1();
}
class Central implement Left,Right
{
public static void main(String arg[])
{
public void m1() // error
{
}
public int m1() // error
{
}
}
}
In the above class same method signature m1 is not allow .violation leads to compile time error.
Exceptional case :
We can’t provide implementation at a time for two or more interfaces having methods with same signature but different return type .
Variable Naming conflicts:
interface Left
{
int x=10;
}
interface Right
{
int x=100;
}
class Central implement Left,Right
{
public static void Main(String arg[]
)
{
System.out.println(x); // reference to x is ambiguous both variables
System.out.println(Right.x);
System.out.println(Left.x);
}
}
we can resolve variable naming conflicts using interface name.(i.e instead of x we have to specify left.x or right.x).
Tag Interface Marker/Ability Interface:
If an interface is marked for some ability such type of interfaces are called maker interfaces or tag interface or ability interface.
Example: Comparable,Serializable,Clonable
If an interface doesn’t contain any method ,it is usually marker interface.Even though interface contains some methods still we can use the term marker interface if this interface is marked for some ability.
Example: Comparable(contains method compareTo())
Adapter classes doesn’t contain concrete methods ,contain only empty implementation of the methods.
Difference between abstract class and interface:
Interfaces provides more security and high performance when compare to abstract classes.
Abstract class may contain concrete methods but an interface never contain any concrete method.
Related Posts...........
Abstract class tutorial and Examples
Inheritance tutorial and Examples
0 comments:
Post a Comment