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 datasource c#?

0 Answers  


Explain different properties of object oriented systems.

0 Answers  


Explain how can you clean up objects holding resources from within the code?

0 Answers  


How to prevent the error while updating ui control from another thread?

0 Answers  


Write a C# program to find the Factorial of n

0 Answers  


If a.equals(b) is true then a.gethashcode & b.gethashcode must always return same hash code.

0 Answers  


3. Use layered architecture for coding. s.no name description 1 abc xxxxxxxxx 2 abc xxxxxxxxx 3 4 5 6 7 8 Select all Clear all Add Delete Name Description Save close

0 Answers  


What is a destructor in c#?

0 Answers  


What is difference between sleep () and wait ()?

0 Answers  


What?s the top .NET class that everything is derived from?

7 Answers  


Why do we use 0?

0 Answers  


Why is it important to override gethashcode when equals method is overridden?

0 Answers  


Categories