简体   繁体   中英

Why base class = new derived class cannot call derived class method?

We have two classes A and B, B Derives from A, I created of A a = new B(); Why in my code when I call method of B that does not contain AI get error. Does it mean that Type Object Pointer is of type A? and second why When I call SomeMethod it calls method of A not B?

In the heap in this situation when created object instance, type object pointer points to A or B?

class A
{
    public void SomeMethod()
    {
        Console.Write("This is A");
    }
}

class B : A
{
    public void SomeMethod()
    {
        Console.Write("This is B");
    }

    public void OtherMethod()
    {
        Console.Write("This is New method");
    }
}
class Program
{
    static void Main(string[] args)
    {
        A a = new B();

        a.SomeMethod(); // This is A method
        a.OtherMethod(); // I get error here that a does not contain definition for this method

    }
}

Because you are missing the glaring warning in the IDE

Severity Code Description Project File Line Suppression State Warning CS0108 'B.SomeMethod()' hides inherited member 'A.SomeMethod()'. Use the new keyword if hiding was intended.

Compiler Warning (level 2) CS0108

'member1' hides inherited member 'member2'. Use the new keyword if hiding was intended.

A variable was declared with the same name as a variable in a base class. However, the new keyword was not used. This warning informs you that you should use new; the variable is declared as if new had been used in the declaration.

If you want to hide it, use the new keyword

However, if you want to call it then use virtual and override it, or just change the method name so you are not hiding it

class A
{
   public virtual void SomeMethod()
   {
      Console.Write("This is A");
   }
}

class B : A
{
   public override void SomeMethod()
   {
      base.SomeMethod();
      Console.Write("This is B");
   }

   public void OtherMethod()
   {
      Console.Write("This is New method");
   }
}

virtual (C# Reference)

The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class. For example, this method can be overridden by any class that inherits it:

override (C# Reference)

The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.

Polymorphism (C# Programming Guide)


In the heap in this situation when created object instance, type object pointer points to A or B?

You need not worry your self about what happens on the heap (these are implementation details), just what the language allows you to do.

However, A technically does not exist, there is only an instance of B, that has all the implementation of A (if you need it)

You have to specify that you want to override a virtual function. Try this:

class A
{
    public virtual void SomeMethod()
    {
        Console.Write("This is A");
    }
}

class B : A
{
    public override void SomeMethod()
    {
        Console.Write("This is B");
    }

    public void OtherMethod()
    {
        Console.Write("This is New method");
    }
}

To get access to OtherMethod where it's currently failing, you need to tell your A to act like a B .

Change

a.OtherMethod();

to

(a as B).OtherMethod();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM