Type casting-Object Type casting
Type cast operator:-
There are two types of type casting are possible in the case of primitives types .
(1) implicit type casting.
(2) explicit type casting.
implicit type casting:-
- Also knowing as “widening”.
- Compiler is responsible for implicit type casting
- Keeping a small value in the bigger container is called implicit type casting.
- No loss of information.
int i=10;
float f=i;
System.out.println(f);//10
Here int value converted to float by compiler.
Explicit type casting:-
- Programmer is responsible for explicit type casting.
- Keeping a big value in the small container.
- It may result loss of information.
- Also know as “narrowing”.
byte b=(byte)i;
System.out.println(b); // - 126
- By explicit type casting, if we assign a big value to the small data type, The most significant bit will be lost.
- If we can typecast a float value into the integral type by explicit type the digits after the decimal point will be lost.
int i= (int)f;
System.out.println(i); // 123
Object type casting:
The parent class reference can be used to hold child class instances.
C obj=new C();
P obj=new C();
From the polymorphism ,we can use like this.If the parent class reference used to hold child class instance by using that reference we are allowed to call only the methods available in the parent class.i.e by using that parent class reference we are not allowed to call child class specific methods.
Example:
class Parent
{
void m1()
{
System.out.println(“parent m1”);
}
void m2()
{
System.out.println(“parent m2”);
}
}
class child extends Parent
{
void m1()
{
System.out.println(“child m1”);
}
void m3()
{
System.out.println(“child m3”);
}
}
public static void main(String ar[])
{
Child c1=new Child();
c1.m1(); // child m1
c1.m2() // parent m2
c1.m3() // child m3
Parent p1=new Child();
p1.m1() // child m1
p1.m2() //parent m2
p1.m3() // compile time error
}
}
Interface reference can be used to hold implemented classes objects.At that time by using that reference we are allowed to call only the methods which are defined in that interface.
Runnable r=new Thread();
r.run(); // valid
r.start() ; // in valid
Runnable r=new Runnable() ; // in valid
Object creation for interface is not possible.
Thread t=new Thread () // valid
Object o=new Object () // valid
General Syntax of object typecasting:
A b=(C) d;
Here A interface or a class
b is the reference variable
C is the class
d is reference or object.
Rule1 for compilation:
The type of c must be same or derived type of “A” other wise violation leads to Compile time error saying incompatible types .
Rule2 for compilation:
The type of ‘d’ must have some relation with ‘c’(either parent –child or child-parent )other wise violation leads to compile time error saying incompatible types.
Examples:
1.String s=(String) (new Object()); //valid
2.String s=(Object)(new String(“abc”); // not valid
3.String s=(StringBuffer)(new Object()); // not valid
4.String s=(StringBuffer)(new String(“abc”)); // not valid
RunTime checking by the JVM:
The underlying object of ‘d’ must be same or derived type of ‘c’;
Examples:
1.Object o1=new String(“laxman”);
String s=(String)o1; // valid
String s=(Object)o1; // not valid
2.Base1 obj=new Der2();
Object obj2=(Base1)obj; // valid
Der2 obj3=(Der2)obj2; // valid
See the Sample codes:
For example If Base1 extends Object
Der1 ,Der2 extends the Base1
3.Base1 obj4=(Der2)obj3; // valid
4.Der1 obj5= (Der1) obj4; // not valid (class cast exception at run time)
5.Object o1=new Object ();
String s=(String)o1; // not valid (class cast exception at run time)
See the Sample codes:
Base1 , Base2 are extends the Object class
Der1 , Der2 extends the Base1 class, Der3 , Der4 extends the Base2 class.
1.Number n=(Integer)(new Byte(b)); // not valid
2.Throwable r=(Exception)(new Error()); // not valid
3.Throwable r=(Exception)(new Exception()); // valid
4.Number n=new Boolean(“true”); // not valid
5.Object o=(Base1)(new Der2()); // valid
6.Base2 o=(Der3)o; // not valid
6.Base1 o=(Der3)(new Der4()); // not valid
Example:
class Dog
{
}
class Beagle extends Dog
{
}
class Kennel
{
public static void main(String arg[])
{
Beagle b1=new Beagle();
Dog d1=new Dog();
Dog d2=b1;
Beagle b3=(Beagle)d2; // valid
Beagle b4=d2; // not vlaid
Beagle b2=(Beagle)d1; // valid
}
}
1 comments:
Thanks! Had a smooth understanding..
Post a Comment