Click below for Step by Step and Interview Q & A (dotnetinter) wrote,
Click below for Step by Step and Interview Q & A
dotnetinter

What is downcasting and upcasting? (.NET interview questions with answers)

www.questpond.com has created an awesome video for this question which is available at this link http://www.youtube.com/watch?v=jpFij6RD7CA

“Upcasting” means moving subclass object to the parent class object. “DownCasting” is opposite to “Upcasting” moving the parent object to the child object.

                                          c

“Upcasting” is perfectly valid but “Downcasting” is not allowed in .NET. For instance below is a simple “Customer” parent class which is further inherited by a child class “GoldCustomer”.

class Customer
{

}

class GoldCustomer : Customer

{

}


Below is an “upcasting” code where the child parent class gold customer is pushed to the customer class.

Customer obj = new GoldCustomer();

Below is a sample of “downcasting” code where parent class object is tried to move to a child class object, this is not allowed in .NET.

GoldCustomer obj = new Customer(); // not allowed illegal

Tags: .net interview questions, c# fresher interview questions, csharp interview questions, downcasting interview questions, it interview questions with answers, programming iterview questions, upcasting
Subscribe

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments