Saturday 3 August 2013

24:Type

Learning Objectives
After completing this session, you will be able to:
‰  Define a “Type”?
‰  Cast Primitive Types
‰  Describe Implicit Type Casting
‰  Explain Explicit Type Casting
‰  Cast Objects
What is a “Type”?
When an object instance is created from a class, you say the object instance is “type” of the class
and its super classes.
Example: Student student1 = new Student();
‰  student1 object instance is the type ofStudent or it is of Student type
‰  student1 object instance is also type of Person if Student is a child class of Person
‰  student1 object instance is also type of Object
Significance of Type
An object instance of a particular type can be used in any place where an instance of the type and
its super type is called for.
Example:
‰  student1 object instance is a “type” of TuftsStudent, Student, and Person
‰  student1 object can be used in any place where object instance of the type of
TuftsStudent, Student, or Person is called for
‰  This enables polymorphism.
Casting Primitive Types
Casting between primitive types enables you to convert the value of one data from one type to
another primitive type. Commonly occurs between numeric types.
There is one primitive data type that you cannotdo casting though, and that is the boolean data
type
Types of Casting:
‰  Implicit Casting
‰  Explicit Casting
Implicit Casting
Suppose you want to store a value of int data type to a variable of data type double.
int numInt = 10;
double numDouble = numInt; //implicit cast
In this example, as the data type (double) of the destination variable holds a larger value than the
data type (int) of the value, the data is implicitly casted to the data type double of the destination
variable.
Implicit Casting: Example
Another example:
int numInt1 = 1;
int numInt2 = 2;
//result is implicitly casted to type double
double numDouble = numInt1/numInt2;
Explicit Casting
When you convert a data that has a large type to a smaller type, you must use an explicit cast.
Explicit casts take the following form:
(Type)value
where, Type is the name of the type you are converting to and value is an expression that results
in the value of the source type.
Explicit Casting: Example
double valDouble = 10.12;
int valInt = (int)valDouble;
//convert valDouble to int type
double x = 10.2;
int y = 2;
int result = (int)(x/y); //typecast result of operation to int
Casting Objects
Instances of classes also can be cast into instances of other classes, with one restriction. The
source and destination classes must be related by inheritance. One class must be a subclass of
the other.
Casting objects is analogous to converting a primitive value to a larger type, some objects might
not need to be cast explicitly.
Cast (classname)object where, classname is the name of the destination class and object is a
reference to the source object
Casting Objects: Example
The following example casts an instance of the class VicePresident to an instance of the class
Employee. VicePresident is a subclass of Employee with more information, which here defines that
the VicePresident has executive washroom privileges.
Employee emp = new Employee();
VicePresident veep = new VicePresident();
// no cast needed for upward use
emp = veep;
// must cast explicitly
veep = (VicePresident)emp;
Implicit Type Casting
An object instance of a subclass can be assigned to a variable (reference) of a parent class
through implicit type casting. This is safe because an object instance of a subclass “is” also the
type of the super class.
Example:
‰  Assume Student class as a child class of Person class
‰  Assume CollegeStudent class as a child class of Student class
‰  CollegeStudent collegeStudent = new CollegeStudent();
‰  Student student = collegeStudent; // Implicit type casting
‰  Person person = collegeStudent; // Implicit type casting
‰  Object object = collegeStudent; // Implicit type casting
Explicit Type Casting
An object instance of a super class must be assigned to a variable (reference) of a child class
through explicit type casting:
Not doing it will result in a compile error because the type assignment is not safe
Compiler wants to make sure that you know what you are doing
Example: Assume Student class is a child class of Person class
Person person1 = new Student();
Student student1 = (Student) person1; // Explicit type casting
Runtime Type Mismatch Exception
Even with explicit casting, you could still end up having a runtime error
Example:
Assume Student class is a child class of Person class
Assume Teacher class is also a child class of Person class
Person person1 = new Student();
Person person2 = new Teacher();
Student student1 = (Student) person1; // Explicit type casting
// No compile error, but runtime type mismatch exception
Student student2 = (Student) person2;
Use instanceof Operator To Prevent Runtime Type Mismatch Error
You can check the type of the object instance using instanceof operator before the type casting.
Example:
Person person1 = new Student();
Person person2 = new Teacher();
// Do the casting only when the type is verified
if (person2 instanceof Student) {
Student student2 = (Student) person2;
}
Summary
‰  Reference Variable Casting:There are two types of reference variable casting:
downcasting and upcasting.
o  Downcasting:If you have a reference variable thatrefers to a subtype object, you
can assign it to a reference variable of the subtype. You must make an explicit
cast to do this, and the result is that you can access the subtype’s members with
this new reference variable.
o  Upcasting:You can assign a reference variable to a supertype reference variable
explicitly or implicitly. This is an inherently safe operation because the assignment
restricts the access capabilities of the new variable.

No comments:

Post a Comment