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

how to retrive a TextBox value in to Sql database using C# windows form application coding

3 Answers  


List down the fundamental oop concepts?

0 Answers  


About delegates and events?

2 Answers   ADITI,


What is list array in c#?

0 Answers  


How can we main a Class as base class,i.e not inherited further?{please Dont tell using Sealed class,any other way}

6 Answers   ACB Group, IBM, Tech Mahindra,






Why do we need events in c#?

0 Answers  


Can struct inherit from class c#?

0 Answers  


What are jump statements in c#?

0 Answers  


Can you use all access modifiers for all types?

0 Answers  


What is session state in asp net c# with example?

0 Answers  


How many digits is a 32 bit number?

0 Answers  


What is the purpose of namespace in c#?

0 Answers  


Categories