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

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the usage of Enumeration in C# Programming and is it good to use or not ?

730


What are virtual classes in c#?

722


What are access modifiers used for?

791


Can we inherit a class with private constructor in c#?

799


Are constructors inherited c#?

659


What is generic delegate in c#?

616


What does return do in unity?

694


Can you store different types in an array in c#?

683


What is meaning of type safety in c#?

632


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

694


What is the difference between finalize() and dispose()?

670


What is object pool in .net?

690


Define c# i/o classes? List the commonly used classes?

723


what is difference between is and as operators in c#?

708


What is inner class in c#?

701