Kokas79
asked on
Inheritance explanations
Hello
Can you explain to me why the following console app produces the results it produces? I just want you to explain how inheritance works with these 2 classes.
class A
{
public void F() { Console.WriteLine("A.F"); }
public virtual void G() { Console.WriteLine("A.G"); }
}
class B: A
{
new public void F() { Console.WriteLine("B.F"); }
public override void G() { Console.WriteLine("B.G"); }
}
class Test
{
static void Main() {
B b = new B();
A a = b;
a.F();
b.F();
a.G();
b.G();
}
Thank you
Can you explain to me why the following console app produces the results it produces? I just want you to explain how inheritance works with these 2 classes.
class A
{
public void F() { Console.WriteLine("A.F"); }
public virtual void G() { Console.WriteLine("A.G"); }
}
class B: A
{
new public void F() { Console.WriteLine("B.F"); }
public override void G() { Console.WriteLine("B.G"); }
}
class Test
{
static void Main() {
B b = new B();
A a = b;
a.F();
b.F();
a.G();
b.G();
}
Thank you
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>so the object B decides to implement the base method because the variable is of type A?
yeah, essentially yes. This behaviour is called "polymorphism" in oop. The method to be implemented is decided on the basis of the type of the object.
go thru following link for more on this..
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/interinher.asp
yeah, essentially yes. This behaviour is called "polymorphism" in oop. The method to be implemented is decided on the basis of the type of the object.
go thru following link for more on this..
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/interinher.asp
ASKER
We are creating an object of type B...right? there is only one new keyword. Then does A a = b mean that we just have a variable of type A that points to object B on the heap?
so the object B decides to implement the base method because the variable is of type A?