What is type casting. Explain it with reference to classes.
Answers were Sorted based on User's Feedback
Answer / kiran vaidya
Type casting means converting some type to some another
type.
for example, there are three classes:
class Employee
{
//Logic for this class
}
class Manager:Employee
{
//Logic for this class
}
class Clerk:Employee
{
//Logic for this class
}
class Program
{
static void Main(string [] args)
{
Employee e=new Employee();
//Type casting
Manager m=(Manager)e;
//Or
Clerk c=(Clerk)e;
}
}
Is This Answer Correct ? | 13 Yes | 2 No |
Answer / shubha
Type Casting is conversion of one type data to another type.
Ex:
public class Base{
//Your code goes here
}
public class Derived extends Base
{
//Inherit method or create your own code here
}
public class MainClass {
public static void main(String args[]){
Base b = new Derived(); //reference variable of Base class points object of Derived class
//Derived d = b; compile time error, requires casting
Derived d = (Derived) b; // type casting Base to Derived
//Here you call your base class methods as derived class methods
}
}
Is This Answer Correct ? | 1 Yes | 0 No |
What is the purpose of a console table?
What is the difference between as and is operators in c#?
explain the three services model commonly know as a three-tier application.
Explain the clr triggers?
What does mean c#?
What if an error occurs in Catch block, how can you prevent it?
Can I call a virtual method from a constructor/destructor?
What is array and arraylist?
What is raise event and what is its use?
Does C# supports multi-dimensional arrays ?
What is default c#?
What is a statement c#?