Thursday, March 30, 2017

Interface in java

Def:-

 Any service requirements speciation is considered as an interface.

Ex :- 

JDBC API acts as requirement specification to develop database driver data base vendor is responsible  to implements this JDBC API.
                       

        
                       
Def 2:- From client point of view an interface defines the set of services what he is expecting.
From service provider point of view an interface defines the set of services what he is offering.
Hence any contract between client and service provider is considered as an interface.
Ex:-Through bank ATM screen bank people are highlighting the set of services what thes are offering .At the same time the same GUI screen represent the set of services what coustomers is expecting hence this GUI screen acts as contract between customer and bank people.


Def:- 

Inside interface evens method is always abstract weather we are not declaring or not hence interface is considered as 100 % pure abstract class

Summary of definition:-

Any service requirement specification or any contract between client and service provider or 100 % pure abstract class is nothing but interface.

Interface declaration & implementation:-

Whenever we are implementation an interface for each and every method of that interface we have to provider implementation otherwise we have to declare class as abstract. Then next level child class is responsible to provide implementation.
Every interface method is always public and abstract weather we are interface method compulsory we should we should declare as public otherwise we all set compile time error.

Ex :-

package com.javahighq;

public interface InsertOf {
void m1();
void m2();
}
abstract class ServiceProvider implements InsertOf{
public void m1(){
      System.out.println("m1 method");
}
}
class SubSemiProvider extends ServiceProvider{

      @Override
      public void m2() {
            System.out.println("m2 method");
      }
     
}

Extend vs Implementation:-

A class can extends only one class at time.
An interface can extends any number of interface simultaneously

Ex:-

public interface InsertOf {
void m1();
void m2();
}
interface Instat{
      void m3();

}
interface SubInterface extends InsertOf,Instat{
     
}



Marker Interface:-

If an interface doesn’t contains any methods and by implementing that interface if out objects will gets some ability such type of interfaces are called Marker interfaces or Ability interfaces or Tag interfaces.

Ex:-

 Serializable (I), Clonnable (I), Remote (I) these are marked for some ability

Ex:-

 By implementing Serializable(I) interface out objects can be saved to the file and can travel across the network.
By implementing Clonable (I) out objects are in a position to produce exactly duplicate cloned objects


Q) Without having any methods how the objects will get some ability in marker interfaces?

A)Internally JVM is responsible to provide required ability.

Q) Why JVM is providing required ability in marker interfaces?

A)To reduce complexity of programming and to make java language as simple

Q) Is it possible to create our own marker interface?


 A) YES, but customized of JVM is required.

No comments:

Post a Comment