Saturday, 25 April 2015

Method Overriding with Exception Handling in Java

There are few things to remember when overriding a method with exception handling. If super class method does not declare any exception, then sub class overriden method cannot declare checked exception but it can declare unchecked exceptions.

Example of Subclass overriden Method declaring Checked Exception

import java.io.*;
class Super
{
 void show() { System.out.println("parent class"); }
}

public class Sub extends Super 
{
 void show() throws IOException //Compile time error  
  { System.out.println("parent class"); } 

 public static void main( String[] args )
 {
  Super s=new Sub();
  s.show();
 }  

}

As the method show() doesn't throws any exception while in Super class, hence its overriden version can also not throw any checked exception.
Example of Subclass overriden Method declaring Unchecked Exception

import java.io.*;
class Super
{
 void show(){ System.out.println("parent class"); }
}

public class Sub extends Super 
{
 void show() throws ArrayIndexOutOfBoundsException      //Correct
   { System.out.println("child class"); }

 public static void main(String[] args)
 {
  Super s=new Sub();
  s.show();
 }  
}
Output : child class

Because ArrayIndexOutOfBoundsException is an unchecked exception hence, overrided show() method can throw it.

More about Overriden Methods and Exceptions

If Super class method throws an exception, then Subclass overriden method can throw the same exception or no exception, but must not throw parent exception of the exception thrown by Super class method.

It means, if Super class method throws object of NullPointerException class, then Subclass method can either throw same exception, or can throw no exception, but it can never throw object of Exception class (parent of NullPointerException class).
Example of Subclass overriden method with same Exception

import java.io.*;
class Super
{
 void show() throws Exception 
  {  System.out.println("parent class");  }
}

public class Sub extends Super {
 void show() throws Exception //Correct    
   { System.out.println("child class"); } 

 public static void main(String[] args)
 {
  try {
   Super s=new Sub();
   s.show();
   }
  catch(Exception e){}
 }  
}
Output : child class

Example of Subclass overriden method with no Exception

import java.io.*;
class Super
{
 void show() throws Exception 
  {  System.out.println("parent class");  }
}

public class Sub extends Super {
 void show()             //Correct    
   { System.out.println("child class"); } 

 public static void main(String[] args)
 {
  try {
   Super s=new Sub();
   s.show();
   }
  catch(Exception e){}
 }  
}

Output : child class

Example of Subclass overriden method with parent Exception

import java.io.*;
class Super
{
 void show() throws ArithmeticException 
  {  System.out.println("parent class");  }
}

public class Sub extends Super {
 void show() throws Exception         //Compile time Error    
   { System.out.println("child class"); } 

 public static void main(String[] args)
 {
  try {
   Super s=new Sub();
   s.show();
   }
  catch(Exception e){}
 }  

}


Saturday, 11 April 2015

Java TreeMap Example


  • A TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class.
  • It contains only unique elements.
  • It cannot have null key but can have multiple null values.
  • It is same as HashMap instead maintains ascending order.


Hierarchy of TreeMap class:
Example of TreeMap class:

package com.mnb;

import java.util.Map;
import java.util.NavigableMap;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

public class TreeMapDemo {

public static void main(String[] args) {
TreeMap<Integer, String> tm = new TreeMap<Integer, String>();

tm.put(1, "Power");
tm.put(2, "Star");
tm.put(4, "Kalyan");
tm.put(3, "Pawan");
System.out.println("Automatically sorted by natural order of Keys from TreeMap:");
for (Map.Entry<Integer,String> me : tm.entrySet()) {
System.out.println(me.getKey() + " : " + me.getValue());
}

System.out.println("To get a reverse order view of Mapping from TreeMap:");

NavigableMap<Integer,String> reverseTreeMap = tm.descendingMap();

Set<Integer> keySet = reverseTreeMap.keySet();
for (Integer key : keySet) {
System.out.println(key + " : " + reverseTreeMap.get(key));
}

// To get first and last entry from TreeMap in Java

System.out.println("First Entry: " + tm.firstEntry());
System.out.println("Last Entry: " + tm.lastEntry());

// To get first and last key from TreeMap in Java

System.out.println("First Key: " + tm.firstKey());
System.out.println("Last Key: " + tm.lastKey());

// Creating subMap from TreeMap in Java
SortedMap<Integer,String> subMap = tm.subMap(3, 5);
System.out.println("SubMap: " + subMap);

// Creating headMap and tailMap from TreeMap in Java

SortedMap<Integer,String> headTreeMap = tm.headMap(3);
SortedMap<Integer,String> tailTreeMap = tm.tailMap(3);
System.out.println("Head Map: " + headTreeMap);
System.out.println("Tail Map: " + tailTreeMap);

}
}

Output:


Automatically sorted by natural order of Keys from TreeMap:
1 : Power
2 : Star
3 : Pawan
4 : Kalyan
To get a reverse order view of Mapping from TreeMap:
4 : Kalyan
3 : Pawan
2 : Star
1 : Power
First Entry: 1=Power
Last Entry: 4=Kalyan
First Key: 1
Last Key: 4
SubMap: {3=Pawan, 4=Kalyan}
Head Map: {1=Power, 2=Star}
Tail Map: {3=Pawan, 4=Kalyan}

Sunday, 5 April 2015

Java LinkedHashMap Example


  • A LinkedHashMap contains values based on the key. It implements the Map interface and extends HashMap class.
  • It contains only unique elements.
  • It may have one null key and multiple null values.
  • It is same as HashMap instead maintains insertion order.
  • You can also create a LinkedHashMap that returns its elements in the order in which they were last accessed.


Hierarchy of LinkedHashMap class:
                               
Example of LinkedHashMap class:

import java.util.LinkedHashMap;

public class LinkedHashMapDemo {
public static void main(String[] args) {

LinkedHashMap<Integer, Integer> linkedMap = new LinkedHashMap<Integer, Integer>();
for (int i = 0; i < 10; i++) {
linkedMap.put(i, i);
}

System.out.println(linkedMap);
// Least-recently used order:
linkedMap = new LinkedHashMap<Integer, Integer>(16, 0.75f, true);

for (int i = 0; i < 10; i++) {
linkedMap.put(i, i);
}
System.out.println(linkedMap);

for (int i = 0; i < 7; i++) {
System.out.println(linkedMap.get(i));
}

System.out.println(linkedMap);
}
}

Output:

{0=0, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9}
{0=0, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9}
0
1
2
3
4
5
6
{7=7, 8=8, 9=9, 0=0, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6}