Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When an object is cast to a base class, how does it remember what it really is?

Tags:

This is a beginner's question, but I am interested in learning what's going on here. My question is, what goes on behind the scenes when you down-cast an object? Does it maintain some sort of metadata about what it originally was? Here's what I mean:

Suppose I have a method called "ClockIn" that accepts a parameter of type "Employee":

public static void ClockIn(Employee employee) {    var manager = employee as Manager;    if (manager != null)    {       manager.OpenSafe();    } } 

So, assume that Manager is a subclass of the Employee type and that it has the "OpenSafe" method:

public class Manager : Employee  {    public void OpenSafe()    {        ...     } } 

The "ClockIn" method, if it finds that a Manager has been passed in, calls the OpenSafe method. Such as:

var bob = new Manager(); ClockIn(bob); 

Here, I've passed in an instance of type Manager into a method that accepts the base class Employee. I need to cast the instance inside the ClockIn method to Manager before I can call OpenSafe.

The question is, is there some metadata that remembers that "bob" is a Manager, even though I've passed him in as an Employee? How does the code know that he can indeed be cast to a Manager? Is there something going on in the heap?

like image 766
Nick R. Avatar asked Oct 25 '10 19:10

Nick R.


People also ask

How do you Upcast an object in C#?

Upcasting can be performed implicitly you don't need any conversion. So just writing Employee emp = mgr; is enough for upcasting. Answer 2 : If you create object of Manager class we can say that manager is an employee. Because class Manager : Employee depicts Is-A relationship between Employee Class and Manager Class.

What is Downcasting and Upcasting in C#?

Upcasting converts an object of a specialized type to a more general type. Downcasting converts an object from a general type to a more specialized type. A specialization hierarchy of bank accounts.

Can we typecast parent to child in C#?

Parent to Child (Explicit casting - Can be successful) Note: Because objects has polymorphic nature, it is possible for a variable of a parent class type to hold a child type. Conclusion : After reading above all, hope it will make sense now like how parent to child conversion is possible(Case 3).


1 Answers

The first thing to remember is that casting does not change the original object at all. It only changes your view of the object through that particular reference. More than one reference can be pointing to the same object, so changing the object isn't a reasonable thing to do on a cast.

What you might do in your instance is to make ClockIn() a method of the Employee class. Then, when you call

bob.ClockIn(); 

then bob will know what type he really is, and call the appropriate ClockIn() method for his type. This is called dynamic method dispatch and is not available for static functions as in your example.

For example:

public class Employee {     public void ClockIn() {         ....     } }  public class Manager: Employee {     public void ClockIn() {         // first, do what all Employees do when clocking in         Employee.ClockIn();         // Next, do Manager specific actions         OpenSafe();     }     public void OpenSafe() {         ....     } } 
like image 199
Greg Hewgill Avatar answered Oct 24 '22 00:10

Greg Hewgill