Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
OBJECT TYPE  CASTING
Type Casting Converting an object of one class to an  object of another class is possible if the  object types are compat...
Eg.,                                PERSON    XY            Z        TEACHER       STUDENT        Z1       Z2             ...
Class definitionpublic Class X                 Class Y extends X {{     public void F();                public void F();  ...
Cont.,Class Z1 extends Z{      public void F();      public void F3a();}                        // Z1,Z2 are subclasses wh...
Cont..1. X x = new X(); //no casting required2. Y y = new Y(); //no casting required3. Z z = new Z(); //no casting require...
Cont..1. X x = new X(); //no casting required2. Y y = new Y(); //no casting required3. Z z = new Z(); //no casting require...
Cont..1. Y yz = new Z(); //incompatible type                     (siblings)2.   Y y1 = new X(); //incompatible. X is not a...
Object type Casting Y yx = (Y) x1; // compiles and runs ok (x1 is type Y) Z zx = (Z) x2; // compiles and runs ok (x2 is ty...
Object Type Casting.,X xz1=new Z1();((Z1)xz1).F();//calls method F() in class Z1((Z1)xz1).F1();//calls method F1() in clas...
THANK YOU!
Upcoming SlideShare
Loading in …5
×

Object type casting

938 views

Published on

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

Object type casting

  1. 1. OBJECT TYPE CASTING
  2. 2. Type Casting Converting an object of one class to an object of another class is possible if the object types are compatible. Java will perform some conversion automatically. Such conversions are known as implicit type casting. Conversions forced by the programmer is known as explicit type casting.
  3. 3. Eg., PERSON XY Z TEACHER STUDENT Z1 Z2 UG_STUD PG_STUD ENT ENT
  4. 4. Class definitionpublic Class X Class Y extends X {{ public void F(); public void F(); public void F1(); public void F2a();} }// X is a super class of Y Class Z extends Xand Z which are sub {classes. public void F(); public void F2b(); }
  5. 5. Cont.,Class Z1 extends Z{ public void F(); public void F3a();} // Z1,Z2 are subclasses which is derived from Z.Class Z2 extends Z{ public void F(); public void F3b();}
  6. 6. Cont..1. X x = new X(); //no casting required2. Y y = new Y(); //no casting required3. Z z = new Z(); //no casting required4. X xy = new Y(); // compiles ok (up the hierarchy)5. X xz = new Z(); // compiles ok (up the hierarchy)6. Y yz = new Z();// incompatible type (siblings)
  7. 7. Cont..1. X x = new X(); //no casting required2. Y y = new Y(); //no casting required3. Z z = new Z(); //no casting required4. X xy = new Y(); // compiles ok (up the hierarchy)5. X xz = new Z(); // compiles ok (up the hierarchy)6. Y yz = new Z();// incompatible type (siblings)
  8. 8. Cont..1. Y yz = new Z(); //incompatible type (siblings)2. Y y1 = new X(); //incompatible. X is not a Y3. Z z1 = new X(); //X is not a Z4. X x1 = y; // compiles ok (y is subclass of X)5. X x2 = z; // compiles ok (z is subclass of X)
  9. 9. Object type Casting Y yx = (Y) x1; // compiles and runs ok (x1 is type Y) Z zx = (Z) x2; // compiles and runs ok (x2 is type Z) // X xy = new Y(); (declared earlier) xy.F(); //calls method F() of class X ((Y)xy).F(); //explicitly type cast xy to method call F() class Y((Y)xy).F2a(); // call the method F2a() from class Y
  10. 10. Object Type Casting.,X xz1=new Z1();((Z1)xz1).F();//calls method F() in class Z1((Z1)xz1).F1();//calls method F1() in class X((Z1)xz1).F2b();//calls method F2b() in class Z((Z1)xz1).F3a();//calls method F3a() in class Z1
  11. 11. THANK YOU!

×