Wednesday, 7 September 2016

class level type casting

Class level type casting:-
The process of converting data from our user defined data type to another user defined data type is called as user defined data type casting (or) class level type casting.
To perform user defined data type casting we must have either extends relation or implements relation between two user defined data types.
There are two types of user data types
1.       Up Casting
2.       Down Casting


1.       Up Casting:-
The process of converting data from sub type to super type is called as “Up Casting”.
In java applications, to perform up casting, we have to assign sub class reference variable to super class reference variable.

Ex code:- 
class A
{
                void m1()
                {
                                System.out.println("m1-A");
                }
}
class B extends A
{
                void m2()
                {
                                System.out.println("m2-A");
                }
}
class Test
{
                public static void main(String[] args)
    {
                   B b=new B();
                   b.m1();
                   b.m2();

                   A a=b;
                   a.m1();
                   //a.m2();--->Error
                   A a1=new B();
                   a1.m1();
                   //a1.m2();---->Error    
                }
}
Output:-
m1-A
m2-A
m1-A
m1-A

When compiler encounters the above assignment statement, compiler will check whether right side variable types in compiler with left side variable type or not, if not, compiler will rise an error like “incompatible type”. If right side variable type is compatible with left side variable type then compiler will rise any error and compiler will not perform type casting.
Note:- 
In java, all sub class types are compatible with the respective super class types, but, super class types are not compatible with the respective sub class types. Due to this reason, we can assign sub class reference variable to super class reference variable, but, we are unable to assign super class reference variable to sub class reference variables.
When JVM encounter the above assignment statement, JVM will perform the following actions.   
1.       JVM will convert right side variable data type to left side variable data type that is “up casting”.
2.       JVM will copy the reference value from right side variable to left side variable.
In java applications, we are able to utilize “up casting”, when we a requirement to access only super clas member over the sub class member in sub class object.
In method overriding, it is mandatory to access super class method, but, we are able to get output from sub class method, to active this requirement, we have to create object for sub class and we have to declare reference variable for super class.
2.       Down casting :-
The process of converting data from super class type to sub class type is called as down casting.
To perform down casting we have to use the following pattern.


Ex :- 
class A
{
                ----
}
class B extends A
{
                ----
}

case-1:
A a=new A();
B b=a;
Status:- compilation Error, incompatible types.
Reason:-
In java application, by default, all the sub class types are compatible with super class types, so that, we can assign sub class reference variables to super class reference variables. In java applications, all super class type are not compatible with sub class types, so that, we are unable to assign super class reference variables to sub class reference variables directly.
Note:-
If we want to assign super class reference variable to sub class reference variable then we have to use “cast operation”.
Case-2:-
A a=new A();
B b=(B)a;
 Status:- No Compilation error, but, java.lang.ClassCastException
Reason:-
In java application, we are able to keep sub class object reference value in super class reference variable, but we are unable to keep super class object reference value in sub class reference variable.
If we are trying to keep super class object reference value in sub class reference variable then JVM will rise an Exception like “java.lang.ClassCastException”.
Case 3:-
A a=new B();
B b=(B)a;
Status:-  No Compilation Error and No Exception.
Ex code 1:-
class A
{
                void m1()
                {
                                System.out.println("m1-A");
                }
}
class B extends A
{
                void m2()
                {
                                System.out.println("m2-B");
                }
}
class Test
{
                public static void main(String[] args)
    {
                   /*
                   A a=new A();
                   B b=a;
                   */
                   /*
                   A a=new A();
                   B b=(B)a;
                   */
                   A a=new B();
                   B b=(B)a;
                   b.m1();
                   b.m2();
    }
}
Output:-
m1-A
m2-B

Ex code 2:-
class A
{

}
class B extends A
{

}
class C extends B
{

}
class D extends C
{

}
class Test
{
     public static void main(String[] args)
     {
                    A a=new A();
                    B b=a;
     }
}
Status:- Compilation Error, incompatible types

Ex code 3:-
class A
{

}
class B extends A
{

}
class C extends B
{

}
class D extends C
{

}
class Test
{
     public static void main(String[] args)
     {
                    A a=new A();
                    B b=(B)a;
     }
}
Status:- No compilation, but, ClassCastException
Ex coed 4:-

class A
{

}
class B extends A
{

}
class C extends B
{

}
class D extends C
{

}
class Test
{
     public static void main(String[] args)
     {
                    A a=new D();
                   D d=(D)a;
     }
}
Status:- no compilation error, No exception.

Ex code 5:-
class A
{

}
class B extends A
{

}
class C extends B
{

}
class D extends C
{

}
class Test
{
     public static void main(String[] args)
     {
                    A a=new D();
                   C d=(D)a;
     }
}
Status:-  No compilation Errors, but NO exception.
Ex code 6:-

class A
{

}
class B extends A
{

}
class C extends B
{

}
class D extends C
{

}
class Test
{
     public static void main(String[] args)
     {
                    A a=new B();
                   C c=(C)a;
     }
}

Output:- No Compilation Error, but, ClassCastException.
Ex code 7:-

class A
{

}
class B extends A
{

}
class C extends B
{

}
class D extends C
{

}
class Test
{
     public static void main(String[] args)
     {
                    A a=new C();
                    B b=(D)a;
     }
}
Status:- No compilation Error, but ClassCastExceptionError.

Ex code 8:-

class A
{

}
class B extends A
{

}
class C extends B
{

}
class D extends C
{

}
class Test
{
     public static void main(String[] args)
     {
                    A a=new D();
                    B b=(C)a;
     }
}
Status:- No Compilation Error, No exception .
Ex code 9:-
class A
{

}
class B extends A
{

}
class C extends B
{

}
class D extends C
{

}
class Test
{
     public static void main(String[] args)
     {
                    A a=new D();
                    D d=(D)(C)(B)a;
     }
}
Status:-  No compilation Error, No exception.


No comments:

Post a Comment