Home>

I am studying c#, but I do not understand how to use abstract methods, so I will ask you a question.

https://qiita.com/igayamaguchi/items/e1d35db0a14a84bda452 of this site
I found the code below, but I understand that the talk method is an abstract method, so I can inherit it, but is it not possible to inherit since the walk method is not an abstract method?

I would also appreciate if you can tell me the difference between abstract classes and interfaces.
Thank you.

code

abstract class AbstractHuman
{
    public void walk()
    {
        // walk process
    }
    // abstract method
    abstract protected void talk();
}
class Human :Abstract Human
{
    protected override void talk()
    {
        // talking process
    }
}
  • Answer # 1

    A lot of information about the net has already rolled about "inheritance", "abstract class", and "interface" related to this question. If you search and understand them,Usually this kind of question does not occur..

    If you haven't searched, let's do it first.
    (The question doesn't mention anything you've looked up about "inheritance", "abstract classes", "interfaces", so it looks like you're not looking at anything)

    If your search doesn't help, add as much as possible to your question what part of which source you didn't understand.

    And, as I'll update, I am making a mistake when asking questions.
    If you find something you don't understand, ask about the wording.

    Can't we inherit since the walk method is not an abstract method?

    Let's see what inheritance is in C#.
    (A reverse question, but reallyOnly abstract methods can be inheritedIs it? Please think)

    Or, try to confirm that the hypothesis is correct by executing it.
    (This is a reverse question, but if you can call the walk method from a Human instance, you can inherit it. Please check if the recognition is correct.)

    Difference between abstract class and interface

    Below are the commentary articles of both parties. Let's check each.

    Abstract class

    interface

    By understanding both, you can sort out which parts are different and which ones have something in common in toukyoutaro's mind.
    (Conversely, what do abstract classes and interfaces have in common? Think about them.)

    To be honest, I don't think they have much in common. In other words, it is obvious that there are differences between the two, so I think you should read and understand each explanation.

  • Answer # 2

    You can only define abstract methods in abstract classes

    Override Modifier-C# Reference | Microsoft Docs

    Cannot override non-virtual or static methods

    It was written.