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 tpl in c#?

507


Explain deadlock?

523


What are the 4 pillars of any object oriented programming language?

462


Why is ienumerable used?

509


What is dbcontext c#?

494






What are synchronous and asynchronous operations?

486


What is anonymous methods in c#?

564


How do you create dlls in .NET

582


Difference between type constructor and instance constructor? What is static constructor, when it will be fired? And what is its use?

553


What is difference between class and interface in c#?

457


If I return out of a try/finally in c#, does the code in the finally-clause run?

562


How to move to a state-related codebase?

905


Explain the accessibility modifier protected internal?

472


What is a web service in c#?

593


Is comparator a functional interface?

469