Object Type Casting.docx

  • Uploaded by: Jitender
  • 0
  • 0
  • December 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Object Type Casting.docx as PDF for free.

More details

  • Words: 221
  • Pages: 6
Object Type Casting: 1. We can use parent reference to hold child object. Eg: Object o = new String(“Durga”); 2. We can use interface reference to hold implemented class object. Eg: Runnable r = new Thread(); A b =(C) d; A: class/Interface name. B: name of ref variable C: class/Interface name. D: ref variable name 3. Rule 1: Compile time checking point 1. The type of “d” and “C” must have some relation either child to parent or parent to child or same type. Otherwise we will get compile time error saying inconvertible types found “d” type required “C”

4. The below example will result in compile time error.

A b =(C) d;

5. Rule 2: Compile Time Checking 2: “C” must be either same or derived type of “A” otherwise we will get compile time error saying incomparable types found “C” required “A” Eg: Object o = new String(“durga”); StringBuffer sb = (StringBuffer) o;

A b =(C) d; 6. Rule 3: Run time object type of ‘d’ must be either same or derived type of ‘C’ otherwise we will get run time exception saying class cast exception. Strictly Speaking: through typecasting we are not creating any new object. For the existing object we are providing another type of reference variable i.e. we are performing but not object casting.

Example 2:

Related Documents

Object Type Casting.docx
December 2019 2
Object
June 2020 12
Object
August 2019 31
Object
November 2019 33
Type
June 2020 37
Object Persistence
November 2019 23

More Documents from ""

Object Type Casting.docx
December 2019 2
Portugues Xxi 2.pdf
December 2019 5
Oops.docx
December 2019 4
Pro-e_file
June 2020 2
Exceptions In Java.docx
December 2019 28