Showing posts with label Interface. Show all posts
Showing posts with label Interface. Show all posts

Wednesday, June 22, 2016

What is the difference between an abstract class and an interface?

What is the difference between an abstract class and an interface?
Ans: 

Abstract Class:

1.An abstract class is written when there are some common features shared by all the objects.
2.When an abstract class is written ,it is the duty of the programmer to provide sub classes to it.
3.An abstract class can contain instance variables also.
4.All the abstract methods of the abstract class should be implemented in its sub classes.

Interface :

1.An interface is written when all the features are implemented differently in different objects.
2.An interface is written when the programmer wants to leave the implementation to the third party.
3.An interface contains only abstract methods.
4.An interface can contain instance variables .It contains only constant.
5.All the methods of the interface should be implemented in its implementation classes.

Multiple Inheritance and Callbacks using Interfaces Example Java

Multiple Inheritance using Interfaces

We know that in multiple inheritance ,sub classes are derived from multiple super classes.If two super classes have same names for their members(variables and Methods) then which member is inherited into the sub class is the main confusion in multiple inheritance.This is the reason ,Java does not support the concept of multiple Inheritance.


Sample Program:

/**
 *
 */
package com.interfaceabhi;

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

 interface Father
 {
float height=6.2f;
void height();
 }

 interface Mother
 {
float height=5.2f;
void height();
 }

 class Child implements Father,Mother
 {

@Override
public void height()
{
float ht=(Father.height+Mother.height)/2;
System.out.println("Child Height= " +ht);
}
 }

public class MultipleInheritanceUsingInterface
{
public static void main(String[] args)
{
Child ch=new Child();
ch.height();
}


}

Result:

Child Height= 5.7

Callbacks using Interface

Calling a function from another function by passing its memory address is called Callbacks.So Callbacks is achieved in java by using interfaces by sharing its memory address.

Sample Program:

/**
 * 
 */
package com.interfaceabhi;

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

interface Tax
{
  double statetax();
}


class AP implements Tax
{

@Override
public double statetax() 
{
System.out.println("According to Ap Gov..");
return 5000.00;
}
}

class Delhi implements Tax
{
@Override
public double statetax() 
{
System.out.println("According to Delhi Gov..");
return 50000.00;
}
}

public class CallBacksUsingInterfaceApp 
{

public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException 
{
Class c=Class.forName(args[0]);
Tax ref=(Tax)c.newInstance();
calculateTax(ref);
}

public static void calculateTax(Tax t)
{
double ct=1000.00;
double st=t.statetax();
System.out.println("Total Tax: " +(ct+st));
}
}




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.