Type-casting-Overview
Type-casting in Java
- Converting one type to another type is known as type casting.
- There are mainly two types:
- Data-type casting
- Class-type casting
Data-type casting
- Converting one data type to another data type is called as data type casting.
- Data-type casting is classified into two types
- Widening / implicit casting
- Narrowing / explicit casting
Widening :
- Casting a lower data type to a higher datatype is known as Widening. The widening will be performed either implicitly or explicitly.
Syntax:
double x = 25; // output-> 25.0
Narrowing :
- Casting a higher datatype to a lower datatype is called Narrowing. Narrowing must happen explicitly in the code otherwise compiler throws an error.
Syntax:
int y = (int) 75.25; // output-> 75
Example:
package com.quipoin;
public class Demo1 {
public static void main(String[] args) {
double x=125; // Widening happens implicitly
//integer type is converted to double type
System.out.println("Value of x:"+x);
System.out.println("------------------");
int y=(int) 8.5; // Narrowing happens explicitly
//double type is converted to integer type
System.out.println("Value of y:"+y);
}
}
Output:
Value of x:125.0
------------------
Value of y:8
Example:
package com.quipoin;
public class Calculator {
static void square(int n) {
int res;
res=n*n;
System.out.println("Square of "+n+" is : "+res);
}
static void square(double n) {
double res = n*n;
System.out.println("Square of "+n+" is : "+res);
}
public static void main(String[] args) {
System.out.println("---------------------------------");
System.out.println("Performing Narrowing Operation!!");
Calculator.square(15);
Calculator.square((int) 25.5); // Narrowing explicitly
System.out.println("--------------------------------");
System.out.println("Performing Widening Operation!!");
Calculator.square(5.5);
Calculator.square(10); // Widening implicitly
}
}
Output:
---------------------------------
Performing Narrowing Operation!!
Square of 15 is : 225
Square of 25 is : 625
--------------------------------
Performing Widening Operation!!
Square of 5.5 is : 30.25
Square of 10 is : 100
Class-type casting
- Converting one class type to another class is known as class-type casting.
- They are classified into two types:
- Upcasting
- Downcasting
Up-casting
- Converting the sub-class type to the superclass type is known as upcasting in other words creating an object of sub-class type and storing its address into super class type.
Syntax :
Demo1 d1 = new Demo2();
Key points:
- Upcasting will be performed either implicitly or explicitly.
- If the compiler performs up-casting on its own then it is implicit up-casting, and if casting is performed in the code by the user then it is called explicit up-casting
- To achieve up-casting inheritance is a must.
Example:
package com.quipoin;
class Demo1 {
int x=10;
void test() {
System.out.println("Running test method!!");
}
}
class Demo2 extends Demo1{
int y=20;
void disp() {
System.out.println("Running disp method!!");
}
}
public class MainD1D2 {
public static void main(String[] args) {
Demo1 d1=new Demo2();//up casting
System.out.println("Value of x : "+d1.x);
d1.test();
}
}
Output:
Value of x : 10
Running test method!!
Note:
- When we perform upcasting inherited members/properties get hidden, only we can fetch superclass members.
Down-casting
- Casting superclass type to sub-class type is known as downcasting,
- In other words, super class type reference is converted to subclass type.
Syntax :
Demo2 d2 = (Demo2) new Demo1;
Key points:
- Downcasting should be performed explicitly in the code by the user.
- To achieve down casting up casting is a must, in other words down casting should be performed only on the upcasted objects otherwise JVM throws ClassCastException.
- When we create an object of subclass it contains its own properties with super class properties, hence we can fetch superclass properties from down casted reference.
Example:
package com.quipoin;
class Demo1 {
int x=10;
void test() {
System.out.println("Running test method!!");
}
}
class Demo2 extends Demo1{
int y=20;
void disp() {
System.out.println("Running disp method!!");
}
}
public class MainD1D2 {
public static void main(String[] args) {
Demo1 d1=new Demo2();//up casting
Demo2 d2=(Demo2) d1;//down casting
System.out.println("Value of x : "+d2.x);
d2.test();
System.out.println("Value of y : "+d2.y);
d2.disp();
}
}
Output:
Value of x : 10
Running test method!!
Value of y : 20
Running disp method!!