Total members 11751 |It is currently Mon Oct 22, 2018 4:39 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





What is the difference between up-casting and down-casting?
Casting in java means converting from type to type. When It comes to the talking about upcasting and downcasting concepts we are talking about converting the objects references types between the child type classes and parent type class. Suppose that we have three classes (A,B,C). Class B inherit from class A. Class C inherit from class B. As follows :
java code
interface A 
{
void display();
}
class B implements A
{
public void display() {
System.out.println("Am in class B");
}

}
class C extends B
{
@Override
public void display() {
System.out.println("Am in class C");
}
}

Note that we have declared the class A as interface but this will not make any changes in illustrating the upcasting/downcasting ideas.. However, It only restricts you from creating an object instance directly from type A.

Applying Up-casting


The upcasting is casting from the child class to base class. The upcasting in java is implicit which means that you don't have to put the braces(type) as a indication for casting. Below is an example of upcasting where we create a new instance from class C and pass it to a reference of type A. Then we call the function display.
java code
public static void main(String[] args) {

// Upcasting from subclass to super class.
A aRef=new C();

aRef.display();//Am in class C


}

Applying Down-casting


The downcasting is the casting from base class to child class. Below we continue on the previous upcasting snippet by adding to lines for downcasting where we down cast the aRef reference from type A to type B. Downcasting is explicit note the usage of braces(type) in the example below.
java code
public static void main(String[] args) {

// Upcasting from subclass to super class.
A aRef=new C();

aRef.display();//Am in class C
//Downcasting of reference to subclass reference.
B bRef=(B) aRef;
bRef.display();//Am in class C

}


The output of the code is :
Code:
Am in class C
Am in class C

Note the display function of class C is called because the type of object is class C

Functions and variables after casting


After casting, you will have access only to the current reference type class members even your object is from type C and your reference of type A. Any unique class member in class C will not be visible to you. For instance :
java code
public class ExampleClass {

public static void main(String[] args) {

// Upcasting from subclass to super class.
A aRef=new C();
aRef.setX(43);// Compile Error

}
}

interface A
{
void display();
}

class B implements A
{

public void display() {
System.out.println("Am in class B");
}

}

class C extends B
{
private int x;
@Override
public void display() {
System.out.println("Am in class C");
}

public void setX(int x) {
this.x = x;
}
}

Common mistake in casting usage :


In downcasting you can't down cast to a inheritance hierarchy level less than your object instance level at creation time. For instance :
java code
public class ExampleClass {

public static void main(String[] args) {

// Upcasting from subclass to super class.
A aRef=new B();

aRef.display();//Am in class C
//Downcasting of reference to subclass reference.
C bRef=(C) aRef; //ERROR
bRef.display();
}
}

interface A
{
void display();
}

class B implements A
{

public void display() {
System.out.println("Am in class B");
}

}

class C extends B
{
@Override
public void display() {
System.out.println("Am in class C");
}

}

In case above your code will compile correctly but you will get runtime error because a created object instance of type B is passed to a reference of type C which is less than B in level. This means that in down-casting we are limited to the original object instance class type while in up-casting we don't have such similar situations.
The output:
Code:
Am in class B
Exception in thread "main" java.lang.ClassCastException: B cannot be cast to C
   at ExampleClass.main(ExampleClass.java:21)
Java Result: 1





Author:

updated.


Author:
Mastermind
User avatar Posts: 2707
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : difference between upcasting and downcasting
 Polymorhism and UpCasting     -  
 difference between the >> and >>> operators     -  
 Difference between forward and sendRedirect     -  
 Difference between PHP echo() and PHP print()?     -  
 find the difference between dates in asp.net     -  
 difference between the String and StringBuffer     -  
 difference between a Window and a Frame     -  
 Difference between Externalization and Serialzation     -  
 difference between a MenuItem and a CheckboxMenuItem     -  
 Difference between throw and throws     -  



Topic Tags

Java Basics






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com