Java- Object Type Casting / Chapter - 10 / OOP's Concept

java Object Type Casting

Now that we have learned that,


We can use parent reference to hold child object.


example:-     Object  o   =  new String ("javabychetan");


We can use interface reference to hold child object implemented class object.


example:-     Runnable      r   =  new Thread ( );


let's understand the syntax of "How to do Type-Casting".

Java Object Type Casting

Before Doing type casting we have to apply or check all these three rules otherwise, we won't able to perform type casting.



Rule 1. (Compile-Time Checking 1)


The type of 'd'  and 'c' must have some relation (either child to parent or parent to child or some other type).Otherwise, we will get Compile time error saying  "incompatible type found:d type required c type.

example 1:-

Object            o           = new      String ("javabychetan");

StringBuffer   sb      = new       (StringBuffer) o ;                 valid type casting

example 2:-

String          s           = new     String ("javabychetan");

StringBuffer   sb      = new      (StringBuffer) s ;

Compile time error saying  "incompatible type found :java.lang.string  required type java.lang.StringBuffer.

Rule 2. (Compile Time Checking 2)


'c' must be either same type or derived type 'A'.Otherwise, we will get Compile time error saying  "incompatible type found:C type required A type.

example 1:-

Object            o           = new      String ("javabychetan");

StringBuffer   sb      = new       (StringBuffer) o ;                 valid type casting

example 2:-

String          s           = new     String ("javabychetan");

StringBuffer   sb      = new      (String) s ;

Compile time error saying  "incompatible type found :java.lang.string  required type java.lang.StringBuffer.

Rule 3. (Run Time Checking )


Runtime object type of' must be either same type or derived the type of 'c' type. Otherwise, we will get runtime exception saying"ClassCastException".

example 1:-

Object            o           = new      String ("javabychetan");

StringBuffer   sb      = new       (StringBuffer) o ;

Runtime Exception Saying"ClassCastException".

example 2:-

Object            o           = new      String ("javabychetan");

String   sb      = new       (String) o ;

this is valid type casting.

Note: Before Doing type casting we have to apply or check all these three rules otherwise, we won't able to perform type casting.


 

 

No comments:

Post a Comment

Be the first to comment!

Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.

Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.

Regards,
JavaByChetan
Back To Home

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com