Wednesday 16 December 2015

Type Casting - Casting One Class to other Class or Interface Example

We often encounter situation where there is a need to store a value of one type into a variable of another type. In such situation, we must cast the value to be stored by processing it. Java provides three ways type cast

  1. Implicit type cast
  2. Explicit type cast
  3. Casting references

Implicit type cast

            When we assign a smaller value to another type of bigger value this type of type casting is possible. Compiler is the responsible to perform this type casting.It is also called widening or promotion or upcasting. In this type of typecasting there is no matter of data loss. This is also know as automatic type casting. The following is possible in java


Example:

        byte b=75;
        int a =b;
are valid statements. Here the byte type is converting into integer type.

Explicit type cast

                          There may be situation where we assign a large variable type to small type. It is the programmer responsibility to handle this type situation. Don't worry about this, Java provides another way to cast large variable type to small type by using a cast operator. It looks like following format 

                  Type variable-1 = (Type) variable-2;

Examples:
         
                       int m=50;
                       byte n=(byte)m;
are valid statements in java. Here the int type is converting into byte type. In this process there may be chance of losing data. The following are various possible conversions where explicit type casting is required

 

Casting One Class to other Class or Interface ( Casting References)

       In java it is also possible to cast one class to other class or interface when there is a relation ship exist between them like inheritance. For example you can convert a sub class object reference into super class object. But we can't convert one class object reference to another without any relation between them. There are two ways to cast references: 1) Generalization 2) Specialization.

Generalization 

                        Generalization is the process where a sub class is promoted to a super class, and hence becomes more general. Generalization needs widening or up-casting. For example, consider the following java program

class one
{
void display1()
{
System.out.println(" super class method");
}
}
class two extends one
{
void display1()
{
System.out.println(" sub class method");
}
}

class cast
{
public static void main( String args[])
{
one obj1; // obje1 is super class reference
obj1= (one) new two(); // obj1 is referencing to sub class object and it widening
obj1.diaply1();
}
}  

Specialization

                      Specialization is the process where a super class is narrowed down to a sub class. Specialization needs narrowing or down-casting.

                               
class one
{
void display1()
{
System.out.println(" super class method");
}
}
class two extends one
{
void display1()
{
System.out.println(" sub class method");
}
}

class cast
{
public static void main( String args[])
{
two obj2; // obje2 is sub class reference
obj2= (one) new one(); // obj2 is referencing to super class object and it is narrowing
obj2.diaply1();
}
}  

 

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top