November 26, 2015

Interface in Java

By using the keyword interface we can fully abstract a class. That is, by using interface we can specify what a class must do, not how it does. By syntax of the interface are similar to the class, but they are not having any instance variables, and also the methods declared in the interface are not having any kind of body structure. In a simple meaning interfaces are generally used to create a kind of blue print.

Once an interface is created any number of classes can implement it and even particular one class can also implements any number of interfaces at any time.

To implement an interface a class must have to create complete set of methods declared in the interface. However each class is free to decide what to implement in the method of the interface.

Interface is generally used to achieve concept of multiple inheritance in java. This is because we can extends only one class at time, which does not allow to extend more than one class at a time. So the interfaces are in introduced in java, where after implements keywords in class we can write any number of interfaces in it. This is how the multiple inheritance is achieved in java.

By default methods declared in interface are public abstract and variables declared in interface are static final.

Difference between abstract and interface:

Difference abstract-interface

Map Interface

Map is very unique and very much useful interface of the collection framework and also a very helpful in very big and complex applications of the java technology.

Map is an interface which maintains data in Key,Value pair. Both key and value are type of Object. Which means we can make any type of data as Key and any type data as Value. But very important thing is that Map contains unique key. Each and every key has some value.

AbstractMap is the abstract class which implements Map interface and HashMap is the concrete sub class of  AbstractMap. TreeMap is an interface which implements SortedMap.

Following is the list of methods provided by Map interface.


  1. put(Object key, Object value)
  2. get(Object key)
  3. size()
  4. clear()
  5. isEmpty()
  6. putAll(Map)
  7. getKey()
  8. getValue()
  9. setValue()


Map.Entry Interface

Map.Entry interface is an interface which is nested interface of Map interface. It generally used for getting key and value from map separately. Following is a simple example which shows the use of Map.Entry interface.

import java.util.HashMap;
import java.util.Set;
import java.util.Iterator;
import java.util.Map;

class MapDemo
{
public static void main(String[] ar)
{
HashMap hm = new HashMap();
hm.put("India",95);
hm.put("China",100);
hm.put("Srilanka",5);
hm.put("Austrila",70);

Set s = hm.entrySet(); // Method 1
Iterator i = s.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
System.out.println("\n");
for(Object o : s) // Method 2
{
System.out.println(o);
}
System.out.println("\n");
for(Object o : s) // Method 3
{
Map.Entry me = (Map.Entry)o;
System.out.println(me.getKey() + " " + me.getValue());
}
}
}

Set Interface

Set is the interface having only a unique set of elements.

Sorted Set is an interface which extends from Set. It extends from collection interface.

Very important thing to remember in Set interface is there are no new methods available in Set other than Collection interface.

AbstractSet is the abstract class which implements Set interface. HashSet is concrete sub class of AbstractSet.

List Interface

A list is an ordered collection sometimes is also known as sequence. List may have contains duplicate values.
AbstractList is an abstract class which implements List interface. ArrayList, LinkedList, Vector are the concrete sub class of AbstractList.

As List interface implements the root interface (which is Collection) it has all the methods of Collection interface but also some new methods are there which is listed below:


  1. void add(int index, Object data)
  2. void set(int index, Object data)
  3. Object get(int index)
  4. Object remove(int index)
  5. int indexOf(Object data)
  6. int lastIndexOf(Object data)
  7. List subList(int start, int end)
  8. ListIterator listIterator()

Now there is a method listIterator() which return the reference of ListIterator. We can also use the simple iterator() of the Collection interface. But the ListIterator have some of the extra facility provided which is not in the Iterator interface.

ListIterator Interface

ListIterator is an interface which extends from Iterator. Using the ListIterator we can also add, set elements at the specified position which is not possible in the Iterator interface.
We can also get an index of the next elements using ListIterator and also can get the previous element while iterating in forward direction.
We can also make iterations in backward directions, which is also not possible in Iterator interface.
Following is the list of the methods provided by ListIterator interface :

  1. hasNext()
  2. next()
  3. remove()
  4. previous()
  5. preivousIndex()
  6. nextIndex()
  7. add()
  8. set()

Collection Framework in java

Java has a very huge variety of feature one of which is Collection interfaces.

Java has a rich Collection interfaces and Classes to use for the developer community. And now a days these collections are very much helpful to the developers for the storing multiple data.

Collection is something like the array but it is too different from the array. Main difference is array stores only one type of data in whole array, like if array is type of integer we can store only numeric values in it where as in a particular collection object we can store any type of data at a time.

Collection enables us to work with the group of objects.

Collection framework have main 3 types of interface

  1. List
  2. Set
  3. Map


Core interfaces of Collection Framework

From the above image we can see that root of Set and List interface is Collection interface. A Map is separate interface but it is to be in the collection framework.

There are some of the common method which is in Collection interface which we can use in all the sub interfaces of the collections. Methods are listed below:

  1. add()
  2. addAll()
  3. clear()
  4. iterator()
  5. remove()
  6. removeAll()
  7. size()