[Article Of The Day: 11 May 2014] Reference type - TopicsExpress



          

[Article Of The Day: 11 May 2014] Reference type casting: Type casting is basically an assignment of a value of one type to a variable of another type. The cast can be to its own class type or to one of its subclass or superclass types or interfaces. casting of object references depends on the relationship of the classes involved in the same hierarchy. As all we know Object class is super class of all classes we can store any class object in reference of Object class. Basically there are two types of casting: 1. Automatic type conversions or Upcasting 2. Explicit type conversions or Downcasting When you are casting up the reference variable to the inheritance tree to a more general class it is called upcasting of variable. Casting of a reference will be along the class hierarchy in a direction from the root class towards subclasses. We can store subclass reference variable in super class reference without any explicit conversion in upcasting. We can also store object of a class in interface reference variable providing that the class should implement the interface. Automatic type conversion will be done when the destination type is larger than source type. Let’s write a sample example to see how this upcasting works. class SuperClass { void myMethod() { System.out.println(OCPJP); } } class SubClass extends SuperClass { } public class NewClass { public static void main(String args[]) { SuperClass ref=new SubClass(); // storing sub class object in super class reference ref.myMethod(); } } In the above example we are storing subclass object in super class reference variable. Using this reference variable we are calling method in super class. The casting is done automatically. Explicit casting also accepted. Now let’s see how casting is done using interface. We can store the object of a class in the reference variable of an interface which is implemented by the class. Interface MyInt { void myMethod(); } class MyClass implements MyInt { void myMethod() { } void myAnotherMethod() { } } Now let’s create a reference variable of MyInt and store the object of MyClass in MyInt reference variable. MyInt ref=new MyClass(); Or MyInt ref=(MyInt)(new MyClass()); Here Myclass object will be casted into MyInt reference. Using this reference we can access the overridden methods of interface i.e we can access only myMethod() we can’t access myAnotherMethod(). When you are casting down the reference variable to the inheritance tree to a more specific class it is called a downcast. To perform conversion between incompatible types, you must need to use variable casting, which performs an explicit conversion between incompatible types. Following example shows you how to do down casting. class Animal { } class Dog extends Animal { } class MyClass { public static void main(String [] args) { Animal a = new Animal(); Dog d = new Dog(); Dog obj1 = a; // wont compile, as downcast needs explicit casting Animal a1 = d; // upcast ok with no explicit casting Dog obj2 = (Dog) a1; // downcast needs explicit casting, works fine. } } When we do down casting we need to do explicit casting.
Posted on: Sun, 11 May 2014 18:02:01 +0000

Trending Topics



Recently Viewed Topics




© 2015