开发者

C# Inheritance concept advise

开发者 https://www.devze.com 2023-01-16 03:50 出处:网络
I have a small query related to OOP concept in C#. I have an interface interface intf { string Hello(); }

I have a small query related to OOP concept in C#.

I have an interface

interface intf
{
   string Hello();
}

A base class

public class BaseClass
{
   public string Hello()
   {
      return "Hello of base class called";
   }
}

A child class that is derived from BaseClass and implements the interface intf as well

public class ChildClass : BaseClass, intf
{
  string  Hello()
  {
     return "Hello of child class called";
  }
}

Now my question is that when I create an object of ChildClass then when I call the hello met开发者_运维问答hod it always calls the hello method of BaseClass. Firstly, why does it call the Hello of the BaseClass? Secondly, how can I call the Hello of the ChildClass?

private void Form1_Load(object sender, EventArgs e)
{
      ChildClass obj = new ChildClass();
      MessageBox.Show(obj.Hello());
}


First of all, you did not provide an access modifier for Hello in ChildClass. This makes it private by default. To access that method from outside of the class, mark it public (or internal if using it from the same namespace). As it stands now, the only publicly visible Hello method is the base class method.

Secondly, once you have resolved the access issue, Hello will hide the method in the base. If this is intentional, it is advisable to use the new keyword with the method

public new string Hello()

If your intention was not to hide the method but instead to override it, mark the method as virtual in the base class and use the override keyword in the child.

public class ChildClass : BaseClass, intf
{
    public override string Hello()
    {
        return "Hello of child class called";
    }
}

public class BaseClass
{
    public virtual string Hello()
    {
        return "Hello of base class called";
    }
}

This will allow you to always call the child method unless you explicitly call the base method from within the child.

Calls like this from the outside world

((BaseClass)child).Hello();
(child as BaseClass).Hello();
BaseClass baseClass = new ChildClass();
baseClass.Hello();

Will result in the child method being used.

Calls like this from within the child class

base.Hello();

Will result in the base class method being used.


Why call the Hello of base

Your child class implements the interface, so it is considered to contain a public Hello method. But in your child class, Hello is not public. The Hello method in base class is public and is considered to be the implementation of the interface.

How to call Hello of child class

Make the Hello in child class public. Then it will be considered to an implementation of the interface.


Declare the method in the base class virtual and then override it in the child class. That way, the virtual call will resolve to the child class instead of the parent class.

Update: To answer the question about why it still the base method even after you put new in the method declaration:

By default, class members are private. Therefore, since you did not mark the method as public, the method is private. Therefore the programs is force to called the base method as it's the only one that's accessible.


The way you've currently declared your methods, the method Hello() in the ChildClass in inaccessible, and hence the public method in the BaseClass is called. What you really want is what Esteban said though, declare the base method public virtual, the derived method public override, otherwise your method is just shadowing the base class method.


As other said, make the Hello method in the Child class public override.

Also, IMHO, you should implement the interface in the base class, not the child class. But that has nothing to do with the fact your method doesn't get called.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号