What is type casting. Explain it with reference to classes.

Answers were Sorted based on User's Feedback



What is type casting. Explain it with reference to classes...

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

What is type casting. Explain it with reference to classes...

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

Post New Answer

More C Sharp Interview Questions

What is raise event and what is its use?

1 Answers   Wipro,


interface a { Method1() Method2() } class b: a { override Method1() override Method2() } what will happen & why?

5 Answers  


the c# keyword .int. Maps to which .net type?

0 Answers   Siebel Systems,


how can i return string by vilating duplicates(inpyt like asdfsda but output should be 2a2d2sf and 2a,2d,2s,f)

2 Answers   Microsoft,


Is c# pass by value?

0 Answers  






Are c# strings null terminated?

0 Answers  


Can variables defined in interface. If yes how we can call/use it in class.

2 Answers   Synechron,


Can namespace contain the private class?

0 Answers  


What is a console file?

0 Answers  


Why multiple Inheritence is not used in C#?

8 Answers   Symphony,


What is Interface and Abstraction (in real time scenario)

4 Answers   HCL,


Where’s global assembly cache located on the system?

0 Answers  


Categories