SlideShare a Scribd company logo
OBJECT TYPE
  CASTING
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.
Eg.,

                                PERSON
    X




Y            Z        TEACHER       STUDENT




        Z1       Z2

                          UG_STUD        PG_STUD
                            ENT            ENT
Class definition

public 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 X
and Z which are sub            {
classes.                             public void F();
                                     public void F2b();
                               }
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();
}
Cont..



1. X x = new X(); //no casting required
2. Y y = new Y(); //no casting required
3. Z z = new Z(); //no casting required
4. 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)
Cont..



1. X x = new X(); //no casting required
2. Y y = new Y(); //no casting required
3. Z z = new Z(); //no casting required
4. 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)
Cont..



1. Y yz = new Z(); //incompatible type
                     (siblings)
2.   Y y1 = new X(); //incompatible. X is not a
                            Y
3.   Z z1 = new X(); //X is not a Z
4.   X x1 = y; // compiles ok (y is subclass of X)
5.   X x2 = z; // compiles ok (z is subclass of X)
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
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
THANK YOU!

More Related Content

PPTX
Db for rdbms
PPTX
Deque and its applications
PPTX
Ajax
PPT
Ot ppt
PPTX
Inner Classes & Multi Threading in JAVA
PPTX
Ascii adjust & decimal adjust
PPTX
Parsing
PPTX
Comparison of pentium processor with 80386 and 80486
Db for rdbms
Deque and its applications
Ajax
Ot ppt
Inner Classes & Multi Threading in JAVA
Ascii adjust & decimal adjust
Parsing
Comparison of pentium processor with 80386 and 80486

Similar to Object type casting (20)

PDF
Chapter 04 inheritance
PDF
javainheritance
PPTX
Inheritance
PPT
Basics of inheritance .22
PPTX
Type casting
PDF
Inheritance
PPT
8 polymorphism
PPT
M251_Meeting 5 (Inheritance and Polymorphism).ppt
PPT
Lecture 14 (inheritance basics)
DOCX
Unit3 java
PPTX
Java Inheritance - sub class constructors - Method overriding
PPTX
Type casting
PDF
C h 04 oop_inheritance
DOCX
Sdtl assignment 03
PPT
06 InheritanceAndPolymorphism.ppt
PPT
InheritanceAndPolymorphismprein Java.ppt
PPT
Cso gaddis java_chapter11
PPTX
Inheritance in java
PPTX
20.2 Java inheritance
PPTX
OO relationships between classes
Chapter 04 inheritance
javainheritance
Inheritance
Basics of inheritance .22
Type casting
Inheritance
8 polymorphism
M251_Meeting 5 (Inheritance and Polymorphism).ppt
Lecture 14 (inheritance basics)
Unit3 java
Java Inheritance - sub class constructors - Method overriding
Type casting
C h 04 oop_inheritance
Sdtl assignment 03
06 InheritanceAndPolymorphism.ppt
InheritanceAndPolymorphismprein Java.ppt
Cso gaddis java_chapter11
Inheritance in java
20.2 Java inheritance
OO relationships between classes
Ad

More from Tech_MX (20)

PPTX
Virtual base class
PPTX
Uid
PPTX
Theory of estimation
PPTX
Templates in C++
PPT
String & its application
PPTX
Statistical quality__control_2
PPTX
Stack data structure
PPT
Stack Data Structure & It's Application
PPTX
Spss
PPTX
Spanning trees & applications
PPTX
Set data structure 2
PPTX
Set data structure
PPTX
Real time Operating System
PPTX
Mouse interrupts (Assembly Language & C)
PPT
Motherboard of a pc
PPTX
More on Lex
PPTX
MultiMedia dbms
PPTX
Merging files (Data Structure)
PPTX
Memory dbms
PPTX
Linkers
Virtual base class
Uid
Theory of estimation
Templates in C++
String & its application
Statistical quality__control_2
Stack data structure
Stack Data Structure & It's Application
Spss
Spanning trees & applications
Set data structure 2
Set data structure
Real time Operating System
Mouse interrupts (Assembly Language & C)
Motherboard of a pc
More on Lex
MultiMedia dbms
Merging files (Data Structure)
Memory dbms
Linkers
Ad

Recently uploaded (20)

PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Machine learning based COVID-19 study performance prediction
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Empathic Computing: Creating Shared Understanding
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Spectroscopy.pptx food analysis technology
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
MYSQL Presentation for SQL database connectivity
Advanced methodologies resolving dimensionality complications for autism neur...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Reach Out and Touch Someone: Haptics and Empathic Computing
Machine learning based COVID-19 study performance prediction
MIND Revenue Release Quarter 2 2025 Press Release
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
20250228 LYD VKU AI Blended-Learning.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Empathic Computing: Creating Shared Understanding
Review of recent advances in non-invasive hemoglobin estimation
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Spectroscopy.pptx food analysis technology
sap open course for s4hana steps from ECC to s4
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...

Object type casting

  • 1. OBJECT TYPE CASTING
  • 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. Eg., PERSON X Y Z TEACHER STUDENT Z1 Z2 UG_STUD PG_STUD ENT ENT
  • 4. Class definition public 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 X and Z which are sub { classes. public void F(); public void F2b(); }
  • 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. Cont.. 1. X x = new X(); //no casting required 2. Y y = new Y(); //no casting required 3. Z z = new Z(); //no casting required 4. 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. Cont.. 1. X x = new X(); //no casting required 2. Y y = new Y(); //no casting required 3. Z z = new Z(); //no casting required 4. 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. Cont.. 1. Y yz = new Z(); //incompatible type (siblings) 2. Y y1 = new X(); //incompatible. X is not a Y 3. Z z1 = new X(); //X is not a Z 4. X x1 = y; // compiles ok (y is subclass of X) 5. X x2 = z; // compiles ok (z is subclass of X)
  • 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. 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