Type Casting 
Types of Data Types 
Primitive or Fundamental Data Types 
Referenced or Advanced Data Types 
Casting Primitive Data Type 
•Widening: Converting a lower data type into higher 
data type is called widening. 
•Narrowing: Converting a higher data type into lower 
data type is called narrowing 
byte short char int long float double
Type Casting… 
Widening in Primitive Data Types: 
char ch = ‘A’; 
int num = (int) ch; 
int x = 9500; 
float sal = (float) x; 
Note: 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”.
Type Casting… 
Narrowing in Primitive Data Types: 
int x = 66; 
char ch = (char) x; 
double d = 12.6789; 
int n = (int) d; 
Note: 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”.
Type Casting… 
Casting Referenced Data Types: 
A class is a 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.
Widening in referenced data type 
class One 
{ 
void show1() 
{ 
System.out.println("I am class one"); 
} 
} 
class Two extends One 
{ 
void show1() 
{ 
System.out.println("I am class Two"); 
} 
} 
class Test 
{ 
public static void main(String args[]) 
{ 
One o; 
o = (One) new Two(); 
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

  • 1.
    Type Casting Typesof Data Types Primitive or Fundamental Data Types Referenced or Advanced Data Types Casting Primitive Data Type •Widening: Converting a lower data type into higher data type is called widening. •Narrowing: Converting a higher data type into lower data type is called narrowing byte short char int long float double
  • 2.
    Type Casting… Wideningin Primitive Data Types: char ch = ‘A’; int num = (int) ch; int x = 9500; float sal = (float) x; Note: 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”.
  • 3.
    Type Casting… Narrowingin Primitive Data Types: int x = 66; char ch = (char) x; double d = 12.6789; int n = (int) d; Note: 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”.
  • 4.
    Type Casting… CastingReferenced Data Types: A class is a 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
  • 5.
    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.
  • 6.
    Widening in referenceddata type class One { void show1() { System.out.println("I am class one"); } } class Two extends One { void show1() { System.out.println("I am class Two"); } } class Test { public static void main(String args[]) { One o; o = (One) new Two(); 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().