OOP Doubt

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I Have this:

class A
{
A()
{
}
}

class B : A
{
B()
{
}
}

Class C
{
C()
{
A foo = new B();
}
}

My question is, what's the type of foo (A or B)
 
Hector,

The variable is of type A, however, it is a reference to an instance of
B. If you call GetType on foo, you will get B.

Hope this helps.
 
Hector Martinez said:
Class C
{
C()
{
A foo = new B();
}
}

My question is, what's the type of foo (A or B)

The type of the variable foo is A. The type of the object foo's value
refers to is B.
 
Everyone else has given you a good answer. I'll just throw in my $0.02
worth to show you what it means:

If I change your code a little:
class A
{
A()
{
}
public virtual string SayHello()
{
return "Hello World, from A!";
}
}

class B : A
{
B()
{
}
public override string SayHello()
{
return "Hi There, from B!";
}
public string SayGoodBye()
{
return "Bye";
}
}

Class C
{
C()
{
A foo = new B();
string test = foo.SayHello();
string test2 = foo.SayGoodBye();
}
}


Exercise:
1. Which value will SayHello() return?
2. Will this compile (assuming no typo's).





Answer:
1. It will return the string in class B. Reason: you really do have a
B, even though you reference it through A. When you call SayHello(), you
will get B's behavior. That behavior is called polymorphism.

2. No, the code will not compile. The reference to B is through a variable
of type A. A does not have a member named SayGoodbye(), so even though you
have a reference to type B, SayGoodBye() is not reachable. this would work,
though:

A foo = new B();
B foo2 = (B)foo;
foo2.SayGoodBye();
 
This might be a bit confusing, no? Another way to put it:

'foo' is a reference to an A. However, since B is-a A (inheritance means
that there is an 'is-a' relation between the two classes), we can have 'foo'
refer to an object whose actual type is B, which is exactly what happens
when you write

A foo = new B();

Sami

Nicholas Paldino said:
Hector,

The variable is of type A, however, it is a reference to an instance of
B. If you call GetType on foo, you will get B.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hector Martinez said:
I Have this:

class A
{
A()
{
}
}

class B : A
{
B()
{
}
}

Class C
{
C()
{
A foo = new B();
}
}

My question is, what's the type of foo (A or B)
 
I appreciate your(everyone) help. But the only thing that I needed to know was the answe that gave me Nicholas. I needed to know if a gettype function take foo as type A or type B.


Thanx everyone. I really appreciate your help.
 
Thats a very good point, and I have recently seen code like this:

A
^
|
B (B descends from A)

if(someObject.GetType() == typeof(A))
{
...
}

It seems the author was really trying to test if passing in an instance of
B, we still want the condition above to succeed and execute the code inside
the if statement. It is a subtle point, but IS should be used in that
instance.

N.


Nicholas Paldino said:
Hector,

The variable is of type A, however, it is a reference to an instance of
B. If you call GetType on foo, you will get B.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hector Martinez said:
I Have this:

class A
{
A()
{
}
}

class B : A
{
B()
{
}
}

Class C
{
C()
{
A foo = new B();
}
}

My question is, what's the type of foo (A or B)
 
Back
Top