Through bank atm , GUI screen bank people are highlighting the set of services what they offer ,at the same time same GUI screen represents the set of services what customers is expecting, hence this GUI screen act s contract between customer and bank people .
Inside interface, every method is always abstract whether we declaring or not, hence interface considered as 100 % pure abstract class.
So what is interface?
* Any service requirement specification or any contract between client and service providers or 100% pure abstract class is nothing but interface.
Interface declaration and implementation
whenever we are implementing an interface for each and every method,after that interface we have to provide the implementation. otherwise, we have to declare a class as abstract. then next level child class is responsible to provide the implementation.
Every interface method is always public and abstract .whether we are declaring or not , hence whenever we are implementing interface method compulsory we should declare as public otherwise we will get compile time error.
interface Interf
{
void m1();
void m2();
}
abstract class ServiceProvider implements Interf
{
public void m1()
{
}
}
class SubServiceProvider extends ServiceProvider
{
public void m2()
{
}
}
Extends vs implements
*Class can extend only one class at a time.
*Interface can extend any number of interface simultaneously
interface A interface B
{ {
} }
interface C extends A, B
{
}
* class can implement any number of interfaces simultaneously
* class can extend another class and can implement any number of interfaces simultaneously
classs A extends B implements C,D,E
{
}
Interface methods
every method present inside the interface is always public and abstract whether we declaring or not.
interface Interf
{
void m1();
}
// public - to make that method available to every implementation class
//abstract- Implementation class is responsible to provide implementation
Hence inside interface , the following method declaration are equal
void m1();
public void m1();
abstract void m1();
public abstract void m1();
Interface variable
an interface contains variables ,the main purpose of the interface variable is to define requirement level constants.
Every interface variable is always public static final whether we declaring or not
interface Interf
{
int x=10;
}
// public - to make that variable available to every implementation class
// static- without existing object , the implementation class can access the variable
//final - if one implementation class change value then remaining implementation class will be affected. to restrict this very interface variable is always final
hence with in the interface the following variable declarionas are wqual
int x=10;
public int x=10;
static int x=10;
final int x=10;
public static int x=10;
public final int x=10;
static final int x=10;
public static final int x=10;
for interface variable compulsory we should perform initialization at the of declaration otherwise we will get compile-time error.
interface Interf
{
int x; // invalid
}
Inside implementation class we can acess interface variables but we can't modify values.
interface Interf
{
int x=10;
}
class Test implements Interf
{
public static void main (String [] args )
{
x=777; // Invalid
System.out.println(x);
}
}
interface Interf
{
int x=10;
}
class Test implements Interf
{
public static void main (String [] args )
{
int x=777; // this is local variable .this is valid
System.out.println(x);
}
}
0 Comments