Type Casting

Converting one data type into another data type is called `type casting` or simply `casting`. Whenever we assign a value to a variable using assignment operator, the java compiler checks for uniformity and hence the data types at both the sides should be same. If data types are not same, then we should convert the types to become same at the both sides. To convert the data type, we use `cast operator`. Cast operator means writing the data type between simple braces, before a variable or method whose value is to be converted.

Casting Primitive Data Types

It is possible to convert one primitive data type into another primitive data type. This is done in two ways, widening and narrowing. The primitive data types are classified into two types, lower types and higher types. Naturally the lower are the types which use less memory and which can represent lesser number of digits in the value.

Implicit Casting

Automatic casting done by the java compiler internally is called implicit casting. Implicit casting is done to convert a lower data type into a higher data type.

Explicit Casting

The casting done by a programmer is called explicit casting. Explicit casting is compulsory while converting from a higher data type to a lower data type.

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 some relationship between them by the way of inheritance. Converting a subclass type into a super class type is called generalization because we are making the subclass to become more general and its scope is widening. This is also called widening or up-casting. Similarly, converting a super class type into a subclass type is called specialization. Here, we are coming down from more general form to a specific form and hence the scope is narrowed. This is called narrowing or down-casting.

Widening In Referenced Data Types

In widening the programmer can access all the super class methods, but not the subclass methods. Widening is converting a lower data type into a higher data type.

Sequence Of Data Types From Lower to Higher

  • Byte
  • Short
  • Char
  • Int
  • Long
  • Float
  • Double
// widening in referenced data types
class One {
    void show1() {
        System.out.println("Super class method");
    }
}

class Two extends One {
    void show1() // override the super class method
    {
        System.out.println("Sub class method");
    }
    
    void show2() // class two specific method
    {
        System.out.println("Sub class method 2");
    }
}

class Cast {
    public static void main(String args[]) {
        One o; // o is super class reference
        o = (One) new Two(); // o is referring to subclass object
        // the above is widening
        o.show1(); // here only `show1()` can be called, but not `show2()` method. As the object `o` is of type `One` and class `One` have only show1() defined in it.
    }
}

The output is

Sub class method

Narrowing In Referenced Data Types

Narrowing is converting a higher data type into a lower data type is called narrowing. It represents converting super class type into subclass type.

// narrowing in referenced data types
class One {
    void show1() {
        System.out.println("Super class method");
    }
}

class Two extends One {
    void show2() // override the super class method
    {
        System.out.println("Sub class method");
    }
}

class Cast {
    public static void main(String args[]) {
        One o;
        o = new Two(); // super class reference to refer to sub class object
        Two t = (Two)o; // this is narrowing
        // type as class Two's type
        t.show1();
        t.show2();
    }
}
The output is 
```sh
Super class method
Sub class method

Subscribe For More Content