SlideShare a Scribd company logo
● Type casting is used to convert an object
or variable of one type into another.
●Assigning a value of one type to a variable
of another type is known as type casting.
Syntax :
dataType variableName = (dataType)
variableToConvert ;
Type Casting
 Types of Data Types
◦ Primitive or Fundamental Data Types
◦ Referenced or Advanced Data Types
• Casting Primitive Data Type
▫ Widening - Converting lower data type into higher
data type is called widening.
▫ Narrowing – Converting a higher data type into
lower data type is called narrowing.
Type casting
WIDENING IN PRIMITIVE DATA TYPES
 Char ch = ‘A’ ;
int num = ( int ) ch ;
int x = 9500 ;
float sal = ( float ) x ;
Widening is safe because there will not be
any loss of data. That is why, compiler does
the casting internally and hence this is called
“Implicit Casting”.
 int x = 66 ;
char ch = ( char ) x ;
double d = 12.6879 ;
int n = ( int ) d ;
Narrowing is not safe because there will be
loss of data. That is why, compiler forces the
programmer to use cast operator and hence
this is called “Explicit Casting”.
Casting Referenced Data Types
 A class is referenced data type. Converting a class type
into another class type is also possible through casting.
But the classes should have the same relationship
between them by the way of inheritance.
University
↑
College
↑
Department
Type Casting
 Generalization : Generalization is a
phenomenon where a sub class is
promoted to a super class, and hence
becomes more general . It needs
widening or up-casting.
 Specialization : Specialization is a
phenomenon where a super class is
narrowed down to a sub class. It needs
narrowing or down-casting.
 Class One
{
void show1()
{
System.out.println (“I am class one“);
}
}
class Two extends One
{
void show1()
{
System.out.println(“I am class Two”);
}
}
Widening in referenced data
types
 ClassTest
{
public static void main{String args[]}
{
One o;
o = (One) newTwo();
o.show1();
}
}
Note : we are able to access show1() method of the super
class. But in this case, it is not possible to call show2().
Type casting
Ad

More Related Content

What's hot (20)

Data types in c++
Data types in c++Data types in c++
Data types in c++
Venkata.Manish Reddy
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
VINOTH R
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
Method overloading
Method overloadingMethod overloading
Method overloading
Lovely Professional University
 
Type conversion
Type  conversionType  conversion
Type conversion
PreethaPreetha5
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
Rokonuzzaman Rony
 
Operators in java
Operators in javaOperators in java
Operators in java
AbhishekMondal42
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
Spotle.ai
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
VINOTH R
 
JVM
JVMJVM
JVM
baabtra.com - No. 1 supplier of quality freshers
 
File handling in c
File handling in cFile handling in c
File handling in c
David Livingston J
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Vineeta Garg
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
Introduction to c#
Introduction to c#Introduction to c#
Introduction to c#
OpenSource Technologies Pvt. Ltd.
 
Java Streams
Java StreamsJava Streams
Java Streams
M Vishnuvardhan Reddy
 
SPL 9 | Scope of Variables in C
SPL 9 | Scope of Variables in CSPL 9 | Scope of Variables in C
SPL 9 | Scope of Variables in C
Mohammad Imam Hossain
 
Applets in java
Applets in javaApplets in java
Applets in java
Wani Zahoor
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
Jin Castor
 
structure and union
structure and unionstructure and union
structure and union
student
 
Loops in c++ programming language
Loops in c++ programming language Loops in c++ programming language
Loops in c++ programming language
MUHAMMAD ALI student of IT at karakoram International university gilgit baltistan
 

Similar to Type casting (20)

Type casting
Type castingType casting
Type casting
simarsimmygrewal
 
Data types in java
Data types in javaData types in java
Data types in java
HarshitaAshwani
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
Swarup Kumar Boro
 
CP Handout#3
CP Handout#3CP Handout#3
CP Handout#3
trupti1976
 
9.0 Typecasting in C Language inc language
9.0 Typecasting in C Language inc language9.0 Typecasting in C Language inc language
9.0 Typecasting in C Language inc language
sukhpreet76
 
Computer science_xii_2016
 Computer science_xii_2016 Computer science_xii_2016
Computer science_xii_2016
Ritika sahu
 
Machine Learning - Dataset Preparation
Machine Learning - Dataset PreparationMachine Learning - Dataset Preparation
Machine Learning - Dataset Preparation
Andrew Ferlitsch
 
DOC-20250124-WA0004._20250430_233059_0000.pptx
DOC-20250124-WA0004._20250430_233059_0000.pptxDOC-20250124-WA0004._20250430_233059_0000.pptx
DOC-20250124-WA0004._20250430_233059_0000.pptx
pawan070201
 
DOC-20250124-WA0004._20250430_233059_0000.pptx
DOC-20250124-WA0004._20250430_233059_0000.pptxDOC-20250124-WA0004._20250430_233059_0000.pptx
DOC-20250124-WA0004._20250430_233059_0000.pptx
pawan070201
 
vb.net.pdf
vb.net.pdfvb.net.pdf
vb.net.pdf
VimalSangar1
 
Typecasting in c
Typecasting in cTypecasting in c
Typecasting in c
Tushar Shende
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
Swarup Boro
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and Variables
Nilesh Dalvi
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
topu93
 
Presentation on c programing satcture
Presentation on c programing satcture Presentation on c programing satcture
Presentation on c programing satcture
topu93
 
unit 1 (1).pptx
unit 1 (1).pptxunit 1 (1).pptx
unit 1 (1).pptx
PriyadarshiniS28
 
Type Casting in java programming language.pptx
Type Casting in java programming language.pptxType Casting in java programming language.pptx
Type Casting in java programming language.pptx
simranpreet12ka4
 
Learn C# Programming - Data Types & Type Conversion
Learn C# Programming - Data Types & Type ConversionLearn C# Programming - Data Types & Type Conversion
Learn C# Programming - Data Types & Type Conversion
Eng Teong Cheah
 
implementing oop_concept
 implementing oop_concept implementing oop_concept
implementing oop_concept
Amit Gupta
 
Introduction to OOPs second year cse.pptx
Introduction to OOPs second year cse.pptxIntroduction to OOPs second year cse.pptx
Introduction to OOPs second year cse.pptx
solemanhldr
 
9.0 Typecasting in C Language inc language
9.0 Typecasting in C Language inc language9.0 Typecasting in C Language inc language
9.0 Typecasting in C Language inc language
sukhpreet76
 
Computer science_xii_2016
 Computer science_xii_2016 Computer science_xii_2016
Computer science_xii_2016
Ritika sahu
 
Machine Learning - Dataset Preparation
Machine Learning - Dataset PreparationMachine Learning - Dataset Preparation
Machine Learning - Dataset Preparation
Andrew Ferlitsch
 
DOC-20250124-WA0004._20250430_233059_0000.pptx
DOC-20250124-WA0004._20250430_233059_0000.pptxDOC-20250124-WA0004._20250430_233059_0000.pptx
DOC-20250124-WA0004._20250430_233059_0000.pptx
pawan070201
 
DOC-20250124-WA0004._20250430_233059_0000.pptx
DOC-20250124-WA0004._20250430_233059_0000.pptxDOC-20250124-WA0004._20250430_233059_0000.pptx
DOC-20250124-WA0004._20250430_233059_0000.pptx
pawan070201
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
Swarup Boro
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and Variables
Nilesh Dalvi
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
topu93
 
Presentation on c programing satcture
Presentation on c programing satcture Presentation on c programing satcture
Presentation on c programing satcture
topu93
 
Type Casting in java programming language.pptx
Type Casting in java programming language.pptxType Casting in java programming language.pptx
Type Casting in java programming language.pptx
simranpreet12ka4
 
Learn C# Programming - Data Types & Type Conversion
Learn C# Programming - Data Types & Type ConversionLearn C# Programming - Data Types & Type Conversion
Learn C# Programming - Data Types & Type Conversion
Eng Teong Cheah
 
implementing oop_concept
 implementing oop_concept implementing oop_concept
implementing oop_concept
Amit Gupta
 
Introduction to OOPs second year cse.pptx
Introduction to OOPs second year cse.pptxIntroduction to OOPs second year cse.pptx
Introduction to OOPs second year cse.pptx
solemanhldr
 
Ad

More from Ruchika Sinha (20)

Python Programming Introduction - Loops & Boolean
Python Programming Introduction  - Loops & BooleanPython Programming Introduction  - Loops & Boolean
Python Programming Introduction - Loops & Boolean
Ruchika Sinha
 
Difference Between Normal & Smart/Automated Home
Difference Between Normal & Smart/Automated HomeDifference Between Normal & Smart/Automated Home
Difference Between Normal & Smart/Automated Home
Ruchika Sinha
 
Greedy Algorithms WITH Activity Selection Problem.ppt
Greedy Algorithms WITH Activity Selection Problem.pptGreedy Algorithms WITH Activity Selection Problem.ppt
Greedy Algorithms WITH Activity Selection Problem.ppt
Ruchika Sinha
 
Greedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.pptGreedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.ppt
Ruchika Sinha
 
Greedy Algorithms Huffman Coding.ppt
Greedy Algorithms  Huffman Coding.pptGreedy Algorithms  Huffman Coding.ppt
Greedy Algorithms Huffman Coding.ppt
Ruchika Sinha
 
DynProg_Knapsack.ppt
DynProg_Knapsack.pptDynProg_Knapsack.ppt
DynProg_Knapsack.ppt
Ruchika Sinha
 
Dijkstra.ppt
Dijkstra.pptDijkstra.ppt
Dijkstra.ppt
Ruchika Sinha
 
Greedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.pptGreedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.ppt
Ruchika Sinha
 
Clipping
ClippingClipping
Clipping
Ruchika Sinha
 
Lec22 intel
Lec22 intelLec22 intel
Lec22 intel
Ruchika Sinha
 
Pc assembly
Pc assemblyPc assembly
Pc assembly
Ruchika Sinha
 
Casing
CasingCasing
Casing
Ruchika Sinha
 
Installation of motherboard
Installation of motherboardInstallation of motherboard
Installation of motherboard
Ruchika Sinha
 
Shortest path
Shortest pathShortest path
Shortest path
Ruchika Sinha
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
Ruchika Sinha
 
Python material
Python materialPython material
Python material
Ruchika Sinha
 
Python3
Python3Python3
Python3
Ruchika Sinha
 
Optimization problems
Optimization problemsOptimization problems
Optimization problems
Ruchika Sinha
 
Regular Grammar
Regular GrammarRegular Grammar
Regular Grammar
Ruchika Sinha
 
Software Teting
Software TetingSoftware Teting
Software Teting
Ruchika Sinha
 
Python Programming Introduction - Loops & Boolean
Python Programming Introduction  - Loops & BooleanPython Programming Introduction  - Loops & Boolean
Python Programming Introduction - Loops & Boolean
Ruchika Sinha
 
Difference Between Normal & Smart/Automated Home
Difference Between Normal & Smart/Automated HomeDifference Between Normal & Smart/Automated Home
Difference Between Normal & Smart/Automated Home
Ruchika Sinha
 
Greedy Algorithms WITH Activity Selection Problem.ppt
Greedy Algorithms WITH Activity Selection Problem.pptGreedy Algorithms WITH Activity Selection Problem.ppt
Greedy Algorithms WITH Activity Selection Problem.ppt
Ruchika Sinha
 
Greedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.pptGreedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.ppt
Ruchika Sinha
 
Greedy Algorithms Huffman Coding.ppt
Greedy Algorithms  Huffman Coding.pptGreedy Algorithms  Huffman Coding.ppt
Greedy Algorithms Huffman Coding.ppt
Ruchika Sinha
 
DynProg_Knapsack.ppt
DynProg_Knapsack.pptDynProg_Knapsack.ppt
DynProg_Knapsack.ppt
Ruchika Sinha
 
Greedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.pptGreedy with Task Scheduling Algorithm.ppt
Greedy with Task Scheduling Algorithm.ppt
Ruchika Sinha
 
Installation of motherboard
Installation of motherboardInstallation of motherboard
Installation of motherboard
Ruchika Sinha
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
Ruchika Sinha
 
Optimization problems
Optimization problemsOptimization problems
Optimization problems
Ruchika Sinha
 
Ad

Recently uploaded (20)

fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
introduction to machine learining for beginers
introduction to machine learining for beginersintroduction to machine learining for beginers
introduction to machine learining for beginers
JoydebSheet
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)
samueljackson3773
 
15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...
IJCSES Journal
 
Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Journal of Soft Computing in Civil Engineering
 
Reagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptxReagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptx
AlejandroOdio
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
introduction to machine learining for beginers
introduction to machine learining for beginersintroduction to machine learining for beginers
introduction to machine learining for beginers
JoydebSheet
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)
samueljackson3773
 
15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...
IJCSES Journal
 
Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
Reagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptxReagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptx
AlejandroOdio
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 

Type casting

  • 1. ● Type casting is used to convert an object or variable of one type into another. ●Assigning a value of one type to a variable of another type is known as type casting. Syntax : dataType variableName = (dataType) variableToConvert ;
  • 2. Type Casting  Types of Data Types ◦ Primitive or Fundamental Data Types ◦ Referenced or Advanced Data Types • Casting Primitive Data Type ▫ Widening - Converting lower data type into higher data type is called widening. ▫ Narrowing – Converting a higher data type into lower data type is called narrowing.
  • 4. WIDENING IN PRIMITIVE DATA TYPES  Char ch = ‘A’ ; int num = ( int ) ch ; int x = 9500 ; float sal = ( float ) x ; Widening is safe because there will not be any loss of data. That is why, compiler does the casting internally and hence this is called “Implicit Casting”.
  • 5.  int x = 66 ; char ch = ( char ) x ; double d = 12.6879 ; int n = ( int ) d ; Narrowing is not safe because there will be loss of data. That is why, compiler forces the programmer to use cast operator and hence this is called “Explicit Casting”.
  • 6. Casting Referenced Data Types  A class is referenced data type. Converting a class type into another class type is also possible through casting. But the classes should have the same relationship between them by the way of inheritance. University ↑ College ↑ Department
  • 7. Type Casting  Generalization : Generalization is a phenomenon where a sub class is promoted to a super class, and hence becomes more general . It needs widening or up-casting.  Specialization : Specialization is a phenomenon where a super class is narrowed down to a sub class. It needs narrowing or down-casting.
  • 8.  Class One { void show1() { System.out.println (“I am class one“); } } class Two extends One { void show1() { System.out.println(“I am class Two”); } }
  • 9. Widening in referenced data types  ClassTest { public static void main{String args[]} { One o; o = (One) newTwo(); o.show1(); } } Note : we are able to access show1() method of the super class. But in this case, it is not possible to call show2().