Tuesday, June 21, 2016

Interface Example Java

What is an Interface?

Ans: An Interface  is a specification of method prototypes.All the methods of the interface are public and abstract.

An interface contains only abstract methods which are all incomplete methods.So it is not possible to create an object to an interface. In this case,we can create separate classes where we can implement all the methods of the interface.These classes are called implementation classes.Since,implementation classes will have all the methods with body ,it is possible to create objects to the implementation classes.The flexibility lies in the fact that every implementation class can have its own implementation of the abstract methods of the interface.

Let us see how how the interface concept  is advantageous in software development.
A programmer is asked to write a Java program to connect to a database and retrieve the data from the database,process the data and display the results in the form of some reports.

class MyClass
{
  void connect()
 {
     //write code to conenct to oracle database
 }

 void disconnect()
 {
    // disconnect from oracle database
 }
}

This class has limitation.It can only connect to Oracle Database.If a client using any other database then this code will not work.So how to solve this problem.we can do this using interface.

interface MyInterface
{
  public void conenct();
  public void disconnect();

}

Why the methods of interface are public and abstract by default?

Ans:  Interface methods are public since they should be available to third party or client to provide implementation .They are abstract because their implementation is left for third party.

Sample Program:

/**
 *
 */
package com.interfaceabhi;

/**
 * @author Abhinaw.Tripathi
 *
 */

interface MyInter
{
  void connect();
  void disconnect();
}

class SQLDB implements MyInter
{

@Override
public void connect() {
// TODO Auto-generated method stub
System.out.println("Connecting to SQL database");
}

@Override
public void disconnect() {
// TODO Auto-generated method stub
System.out.println("disconnecting from SQL database");
}

}

class Oracle implements MyInter
{

@Override
public void connect() {
// TODO Auto-generated method stub
System.out.println("Connecting to Oracle database");
}

@Override
public void disconnect() {
// TODO Auto-generated method stub
System.out.println("disconnecting from Oracle database");
}

}


public class IntrefaceDemo
{
/**
* @param args
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException
{
  Class c=Class.forName(args[0]);
  MyInter myIn=(MyInter)c.newInstance();
  myIn.connect();
  myIn.disconnect();
}

}

Another Sample Program:

/**
 * 
 */
package com.interfaceabhi;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

/**
 * @author Abhinaw.Tripathi
 *
 */
interface Printer
{
  void Printit(String text);
  void disconnect();
  
}

class IBMPrinter implements Printer
{

@Override
public void Printit(String text)
{
 System.out.println(text);
}

@Override
public void disconnect() 
{
System.out.println("Printing Completed!!");
System.out.println("Disconnecting from IBM printer");
}
}

class EpsonPrinter implements Printer
{

@Override
public void Printit(String text) 
{
System.out.println(text);
}

@Override
public void disconnect() 
{
System.out.println("Printing Completed!!");
System.out.println("Disconnecting from Epson printer");
}
}

public class PrinterInterfaceDemo {

/**
* @param args
* @throws IOException 
* @throws ClassNotFoundException 
* @throws IllegalAccessException 
* @throws InstantiationException 
*/
public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
{
 FileReader fr=new FileReader("config.txt");
 LineNumberReader lnr= new LineNumberReader(fr);
 String printername=lnr.readLine();
 System.out.println("Loading the driver from : "+printername);
 Class c=Class.forName(printername);
 Printer ref=(Printer)c.newInstance();
 ref.Printit("Hello,This is printed on the printer");
 ref.disconnect();
}

}

Summary:

  • An interface is a specification of methods prototypes.This means,only method names are written in the interface without method bodies.
  • An interface will have 0 or more abstract methods which are all public and abstract by default.
  • It can have variables which are public static and final by default.This means all the variables of the interface are constants.
  • No private,protected or static methods are allowed.
  • We can not create object to an interface  but we can create reference.
  • An Interface can extend another Interface.
  • An Interface can not implement another Interface.
  • It is possible to write a class within an interface.

No comments: