Loading
Type-casting-Overview

Type casting is the process of converting one data type into another. In Java, type casting can be divided into two main types:

1.  Data-type casting
2.  Class-type casting

1. 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 (Implicit Casting):

When a smaller data type is converted into a larger data type, it is called widening or implicit casting. This happens automatically as there is no risk of data loss.


Syntax:

// Implicit conversion from int to double double x = 25; System.out.println(x); // Output: 25.0


Narrowing (Explicit Casting):

Casting a larger data type to a smaller data type is called narrowing or explicit casting. Since there is a possibility of data loss, it must be done explicitly by the programmer.


Syntax:

// Explicit conversion from double to int int y = (int) 75.25; System.out.println(y); // 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: here we see both 'widening' and 'narrowing'

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

2. Class-type Casting

Class-type casting involves converting one object type to another within an inheritance hierarchy. It is divided into:

  • Upcasting
  • Downcasting

Upcasting

Converting the subclass 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 : 

Superclass obj = new Subclass();



Key Point

  • 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.

Downcasting

Converting a superclass object back to a subclass type is called downcasting. This must be done explicitly using type casting.


Syntax :

Subclass obj = (Subclass) superclassReference;



Key Point

  • 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!!


Two Minute Drill

  • Widening (Implicit Casting): Converts smaller data types to larger ones automatically.
  • Narrowing (Explicit Casting): Converts larger data types to smaller ones with explicit instructions.
  • Upcasting: Converts a subclass object to a superclass type.
  • Downcasting: Converts a superclass object back to a subclass type but requires explicit casting.