Recent

12.Upcasting and downcasting in Java

Upcasting in Java

The Reference ID of a child class object can be put into the reference variable of parent class. But vice versa is not true. And this concept is known asupcasting.

For Example(Syntax):

Suppose we defined a class “Base” and one another class with name “Child”. We will create the object of child class but we will use the reference variable of parent class to store the reference of Child’s object.

Base b = new Child();  //upcasting

An example of upcasting:

 

 

class Base

{

void show()  //method in parent class

{

System.out.println("Show from parent class");

}

}

 

class Child extends Base

{

 

public static void main(String[] art)

{

Base b = new Child();  //upcasting

b.show();  //calling parent show with parent variable

}

 

}

Output:

If we do upcasting then we cannot access the personal method of a child class by the reference variable of parent class. Check this example to understand this concept:

 

class Base

{

 

}

 

class Child extends Base

{

 

void show()

{

System.out.println("Show from child class");

}

 

public static void main(String[] art)

{

Base b = new Child();

b.show();

}

 

}

Output:

 

In this program we defined the show method in child class instead of parent class. So, we are getting a compilation error. Because the compiler will search for method existence in Parent class only at compile time.

 

In case of method overriding

·          If a child class has overridden a parent class method then with the child class object always the overridden method is called.

·         It hardly matters whether the RID of a child class object into the child class reference variable or into parent class reference variable.

See this Example for proof:

 

 

class Base

{

void show()  //method show() defined in parent class

{

System.out.println("Show from parent class");

}

}

 

class Child extends Base

{

 

void show()  //method overridden

{

System.out.println("Show from child class");

}

 

 

public static void main(String[] art)

{

Base b = new Child();  //upcasting

b.show();  //calling show method

}

}

Output:

Downcasting in Java

Downcasting is to put the child class object reference ID back into the child class object by using the parent class reference variable.

Upcasting is required before downcasting.

Syntax:

Base b = new Child();  //syntax for upcasting

Child c = (Child)b;  //syntax for downcasting

From the downcasting statement, the RID of child class is now in the reference variable of child class. And now we can access the child class personal methods.

An example of Downcasting:

 

 

class Base

{

 

}

 

class Child extends Base

{

void show()

{

System.out.println("Child");

}

 

public static void main(String[] args)

{

Base b = new Child();  //upcasting

 

Child c = (Child)b;  //downcasting

 

c.show();  //accessing child's method independently

}

}

 

/* Output

Child

*/

No comments:

Post a Comment

Bug-dbug Designed by Bugdbug - Developed by Belson Raja Copyright © 2016

Theme images by Bim. Powered by Blogger.
Published By Bugdbug India