Add to Google Reader or Homepage

java.lang.classcastexception

ClassCastException:

      The java.lang.ClassCastException is one of the common exception that is experienced by beginners.Let us know in detail what does it mean?, When it is thrown?and how to fix this exception in this post.

What is meant by java.lang.classcastexception?

You might all be familiar with the word Cast in the programming context.In most of the programming languages Cast refers to conversion.In this exception is is given that Class Cast Exception.So it indicates  that in our program we have tried to "convert an object of one class type in to an object  of another class type".

Note:

There is one possibility in casting an object,"If a class extends a parent class then the child class object can be casted to its parent class".The reverse process is not possible in most of the programming languages.

Look at the following snippet to understand it:

Class Parent
{
..........
...........
}
Class Child extends Parent
{
...........................
}

Class Sample
{
......
Child c=new Child();
Parent p=c;                //converting an object of child type to its parent type
}

In this sample program i have tried to cast an object of class Child to its Parent class type.This is a legitimate casting.

But the reverse casting is not allowed that is

Child c=new Child();
Parent p=new Parent();
c=p; // casting an object of parent type to its child type which is not allowed


When will be this exception is thrown?

There are two main occasions on which this exception will be thrown.

1.As i have already told when you try to cast an object of Parent class to its Child class type this exception will be thrown.

2.When you try to cast an object of one class into another class type that have not extended the other class or they don't have any relationship between them.

See the example,

Class A                        
{
}

Class B
{
}

B bobject=new B();
A aobject=new A();

aobject=(A)bobject; //throws an ClassCastException

Reason: Trying to cast an object of class B to the type A.

Examples like trying to cast an String object into an Integer will throw this classcastexception.

Example Situation for this exception:

import java.util.*;
import java.io.*;

class Parent
{
String name;
Parent(String n)
{
name=n;
}
public void  display()
{
System.out.println(name);
}
}
class Child extends Parent
{
String cname;
Child(String name)
{
super(name);
cname=name;
}

public void display()
{
System.out.println(cname);
}

}

class Sample1
{
public static void main(String args[])
{
Child c=new Child("hello");
Parent p=new Parent("hai");
p=c;
p.display();
Parent p1=new Parent("world");
Child c1=(Child)p1;
}
}

output:

hello
Exception in thread "main" java.lang.ClassCastException: Parent cannot be cast t
o Child
        at Sample1.main(Sample1.java:42)

As you see the output of this program we can able to understand that i have tried to cast an object of Parent class to its Child class  type.


Example 2:

import java.util.*;
import java.lang.*;

class Listimpl{
public Listimpl(T a)
{
i=(List)a;
i.get(0).intValue();
}
private List i;
}

class Castimpl
{
public static void main(final String[] args)
{
  List s = new LinkedList();
s.add("jesus");
Listimpl i = new Listimpl(s);

}
}



This kind of exceptions also arises when we are dealing with Generic Programming.Look at the above program to understand this problem.

In the above program, I have anticipated for the List of Integer type in the constructor Listimpl but actually i have created the object for Listimpl as String type.

Since i have used List as the cast type(i=List(a);) the compiler would not bother about the type of the List that we are assigning to the List i of Integer type.

So it does not shows any compilation error while assigning to an list of Integer object type.

As the value stored in that list is a string object, when we try to convert it into an integer value using the intValue() method, class cast exception is thrown.

This kind of exceptions usually occur in the situations like,we may be anticipating an integer object from the client system but we actually received the String object,we may expect integer values as the result of a query from the database but the actual value stored in the database may be a String value.

Solution:

So,when trying to cast an object of one class type into another class type ensure that the type to be casted is of its parent type.

I hope this may avoid this classcastexception to be thrown...



0 comments:

Post a Comment

 
java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top