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

Similar to Object type casting (20)

PDF
Chapter 04 inheritance
Nurhanna Aziz
 
PDF
javainheritance
Arjun Shanka
 
PPTX
Inheritance
Sapna Sharma
 
PPT
Basics of inheritance .22
myrajendra
 
PPTX
Type casting
simarsimmygrewal
 
PDF
Inheritance
Rahul Hada
 
PPT
8 polymorphism
Abhijit Gaikwad
 
PPT
M251_Meeting 5 (Inheritance and Polymorphism).ppt
smartashammari
 
PPT
Lecture 14 (inheritance basics)
Abhishek Khune
 
DOCX
Unit3 java
mrecedu
 
PPTX
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
PPTX
Type casting
Ruchika Sinha
 
PDF
C h 04 oop_inheritance
shatha00
 
DOCX
Sdtl assignment 03
Shrikant Kokate
 
PPT
InheritanceAndPolymorphismprein Java.ppt
gayatridwahane
 
PPT
06 InheritanceAndPolymorphism.ppt
ParikhitGhosh1
 
PPT
Cso gaddis java_chapter11
mlrbrown
 
PPTX
Inheritance in java
Tech_MX
 
PPTX
20.2 Java inheritance
Intro C# Book
 
PPTX
OO relationships between classes
Sujit Kumar
 
Chapter 04 inheritance
Nurhanna Aziz
 
javainheritance
Arjun Shanka
 
Inheritance
Sapna Sharma
 
Basics of inheritance .22
myrajendra
 
Type casting
simarsimmygrewal
 
Inheritance
Rahul Hada
 
8 polymorphism
Abhijit Gaikwad
 
M251_Meeting 5 (Inheritance and Polymorphism).ppt
smartashammari
 
Lecture 14 (inheritance basics)
Abhishek Khune
 
Unit3 java
mrecedu
 
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
Type casting
Ruchika Sinha
 
C h 04 oop_inheritance
shatha00
 
Sdtl assignment 03
Shrikant Kokate
 
InheritanceAndPolymorphismprein Java.ppt
gayatridwahane
 
06 InheritanceAndPolymorphism.ppt
ParikhitGhosh1
 
Cso gaddis java_chapter11
mlrbrown
 
Inheritance in java
Tech_MX
 
20.2 Java inheritance
Intro C# Book
 
OO relationships between classes
Sujit Kumar
 

More from Tech_MX (20)

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

Recently uploaded (20)

PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Ad

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