Home | Login | Blog | Forum | Download | RSS Feed | Ajax | Hello-World | J2EE | BPM | EJB | FileNet | Hibernate | ORM | IBM | ILOG | Interview | FAQs |

Java | Apache | JVM | Others | BRMS | Oracle | Pega | SOA | Sun-Certification | Web-Service | websphere | XML |
 

JAVA DEVELOPER SITE - COMPLETE JAVA TUTORIALS

Java Type Casting, Type Casting in Java - A Java Tutorial


Share
 
 

Useful Java Links


       


Java Object type casting

Converting object reference of one type to another type is called Java object typecasting. Casting is a programming term that means, effectively, converting a value or an object from one type to another. The result of a cast is a new value or object; casting does not change the original object or value.

The class of the object you're casting and the class you're casting it to must be related by inheritance; that is, you can cast an object only to an instance of its class's sub- or superclass-not to any random class. Casting downward in the class hierarchy is automatic, but casting upward is not.

 There are two types of object typecasting:
1. upcasting 
2. downcasting

Upcasting:
 Upcasting is implicit and safe. Here subclass object is converted to superclass object, because a subclass object is also a superclass object.

Downcasting:
 Down-casting is potentially unsafe, because you could attempt to use a method that the derived class does not actually implement. With this in mind, down-casting is always explicit, that is, we are always specifying the type we are down-casting to. Converting an instance of a subclass to an instance of a superclass loses the information

Upcasting vs Downcasting

Given Below is a program which easily demonstrates the difference between Upcasting and downcastin in Java.

_____________________________________________________

Vehicle v1  = new Car();  / /Right . upcasting or implicit casting
Vehicle v2= new  Vehicle();

Car c0 = v1;  // Wrong compile time error "Type Mismatch"
  //Explicit or downcasting is required
Car c1 = (Car) v1 // Right. downcasting or explicit casting . v1 has a knowledge    //of Car due to line 1
Car c2= (Car) v2;  //Wrong Runtime exception ClassCastException because v2    //has no knowledge of Car.
Bus b1 = new BMW();  //Wrong compile time error "type mismatch"
Car c3 = new BMW(); //Right. Upcasting or implicit casting
Car c4 = (BMW) v1; // Wrong Runtime exception ClassCastexception
Object o = v1;  //v1 can only be upcast to its parent
Car c5 = (Car) v1; // v1 can be downcast to Car due to line 1
    _____________________________________________________

  • ClassCastException is thrown to indicate that code has attempted to cast an object to a subclass of which it is not an instance.  Use exception handling mechanism to catch ClassCastException
  • Use the instanceof operator to guard against incorrect casting.

 




Download Latest Java/J2EE eBooks, SCJP Dumps, SCWCD Dumps, Pega Tutorial, IBM WebSphere BPM tutorials and many more


If you need any urgent assistance on java-object-type-casting-tutorial, kindly email your requirement to us at : info@javagenious.com or Contact-an-Expert. Our experts will try their best to solve your problem.

You can also subscribe to our newsletters on java-object-type-casting-tutorial, to receive updates on java-object-type-casting-tutorial,via email.Enter you email below:

Enter your email address:

Delivered by FeedBurner



Keyword Tags: java-object-type-casting-tutorial tutorial,java-object-type-casting-tutorial in java,Concepts of java-object-type-casting-tutorial,Java,J2EE,Interview Questions,java-object-type-casting-tutorial Examples


Comments:


Post Your Comment:

*

Yes, I would like to recieve email notifications on my reply.

Popular Posts:

 
Popular Downloads

SCJP 1.6 Dumps
JSP Interview Questions
Struts Interview Questions
Hibernate Interview Questions
SCWCD Dumps
SCBCD Dumps

Java/J2EE Tutorial
JMS Tutorials
seam richfaces tutorial
richfaces live demo
jquery example demo
JQuery Tutorial and Source Code
JSF Tutorial
XSL Tutorial
J2EE JMS
CORBA Applications
Java CERTIFICATION
SCJP 1.5
SCJP 1.6
SCWCD 1.5
SCBCD
Mis
Ajax autosuggest autocomplete from database
Struts forms and validation with webflow
Java Apache POI Examples
JDBC Tutorial
Oracle 9i & 10g
MYSQL Tutorial
php Tutorial
JQuery Examples
Upload Java File
JAXB Examples
PL-SQL Tutorial
Dojo AJAX
EJB Examples
Java IDE
Eclipse
NetBeans
IBM Rational Weblogic Workshop
Downloads
SCJP 1.6 Dumps
SCJP 1.5 DUMP
SCJP Material
SCWCD
Java Security
SCBCD Dumps
External Links
SUN Java
JOracle
IBM
PHP Certification
Technical Interview Questions
Amazon Interview Questions
Google Interview Questions
Microsoft Interview Questions
IBM Interview Questions
Yahoo Interview Questions
All Tutorials
php mysql tutorial
SCJP 1.5/1.6
SCWCD 1.5/1.6
Polymorphism
Thread Tutorial
Generics & Exceptions
Spring Struts Hibernate
Java Collections
MAP
SET
LIST
VECTOR
Advanced Java
Stateless Session Bean
Introduction to EJB
Stateful Session Bean
Java Design Patterns
HOME | FORUM | ABOUT | BLOGS | CONTACT-US