Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
Hi,

I have a simple question. Suppose I have two classes like below.
class Room
{}

class LivingRoom : Room
{
    int p1;
    public void Method()
    {
    }
}


And suppose I am creating a new instance of class LivingRoom in my main program and assigning it to Room.

C#
static void Main(string[] args)
        {
            Room r1 = new LivingRoom();
            Console.ReadLine();
        }


When a LivingRoom instance is created, there should be at least unassigned pointers to property p1 and method Method() of LivingRoom object in the memory. But there are no corresponding properties and methods in Room class. Should not there be an error as there are not such properties and method in the base class ? There is no pointer in Room class or a property to hold what exists in the inherited class LivingRoom ?

Regards,
Posted
Comments
johannesnestler 26-Feb-11 7:04am    
Hi Close Network,

What unassigned pointers? int p1 is a value type and gets its default value on construction - you construct a LivingRoom object with new - so also your method has an address... then you assign your LivingRoom instance to a base class reference of type Room - the instance itself has all the propertys and methods (you can later cast back to a LivingRoom object, no?), you just decided not to use them by holding the reference to LivingRoom as a base type. Maybe some book reading would be a better fit - read about inheritance. This kind of behavior is basic OOP and (in all programming languages I know) the same.
Close Network 26-Feb-11 7:17am    
Considering the method only, since int is value type, the newly created object has a reference to its method and this does not have to be assigned to corresponding base object method reference, as the reference to LivingRoom object is assigned to r1 an this already holds the reference to the method too. Is this what you mean ?

When you assign your LivingRoom to "r1", you do not assign a Room to it: you assign an inherited class, it is not truncated or otherwise converted to a Room. The Room variable "r1" will still work as a Room, you can access all of the properties and methods of a Room, but you cannot access the properties or methods of a LivingRoom until you have cast the variable back to a LivingRoom:

C#
using System;

namespace ConsoleTester
    {
    public class Room
        {
        public int r;
        }

    public class LivingRoom : Room
        {
        public int p1;
        public void Method()
            {
            }
        }
    class Program
        {
        static void Main(string[] args)
            {
            Room r1 = new LivingRoom();
            r1.r = 14;    // Compiler is happy
            r1.p1 = 14;   // Compiler complains: "Room does not contain a definition of p1"
            ((LivingRoom) r1).p1 = 14; //Compiler is happy.
            Console.ReadLine();
            }
        }
    }



"The problem is in casting. When r1 is cast to inherited type, it must be holding a reference to the Method() of LivingRoom object. That it is just hidden does not mean that there exists no reference to it. Is this correct ?"

Not really: you are thinking about it wrong.

"r1" holds a reference to a Room all the time. It doesn't know about anything outside the Room class. That it actually holds a reference to a LivingRoom instance is irrelevant to it: it doesn't even know that LivingRooms exist! The LivingRoom instance has all the info it needs to be a LivingRoom, as well as all the info to be a Room.
 
Share this answer
 
v2
Comments
Close Network 26-Feb-11 7:47am    
The problem is in casting. When r1 is cast to inherited type, it must be holding a reference to the Method() of LivingRoom object. That it is just hidden does not mean that there exists no reference to it. Is this correct ?
OriginalGriff 26-Feb-11 7:55am    
Answer updated
Sergey Alexandrovich Kryukov 26-Feb-11 11:50am    
My 5. Maybe, my Answer is more explaining as I try to understand what's going on with the "pointers"
--SA
You apparently unaware of the very object-oriented fundamentals.

First to understand: there are two different types involved: compile-time and run-time type.

C#
class MyBaseClass { internal byte somethingOld; }
class MyDerivedClass : MyBaseClass { internal int somethingNew; }

MyBaseClass b = //the variable is of complile-time class MyBaseClass
    new MyDerivedClass(); //
bool isDerived = b is MyDerivedClass; //returns true

//can be down-casted:
MyDerivedClass derived = b as MyDerivedClass; //could be null, but it is not
isDerived = derived != null; //still true
derived.somethingNew = 13; //will work


You "pointer" terminology is incorrect for .NET, but let's forger about it for a while: you can thing of .NET references as of "pointers", to a certain point. The instance points to a block of memory. When you create an instance of MyBaseClass, you have only somethingOld in this block, not somethingNew. When you create derived class, the block of memory looks exactly the same, but somethingNew is appended to it. A variables point to united block of memory. From the standpoint of the variable b it points to smaller block, but at the same time it points to extended block of memory; common part is the same as that of MyBaseClass.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900