C#

inheritance : class VS interface

Naranjito 2023. 2. 24. 00:50
  • class

It can inherit to other class only one class.

The abstract class needs to use 'override'.

 

  • interface

It can inherit to multiple class.

It does not need to use 'override'.

 

Ex)

1. Create the abstract class(parents). (uncompleted method)

2. Create the interface. Using with 'I' in accordance with the custom. (uncompleted method)

3. Create the class(children) which is inherited 1,2.

# 1
abstract public class A : MonoBehaviour
{
    abstract public void Abc();
}
# 2
interface Ipart 
{
    void Bbc();
}
# 3
public class part : A, Ipart
{
    public override void Abc() //'override'is must from abstract class
    {
        print("Abc");
    }
    public void Bbc() //interface class does not need to 'override'
    {
        print("Bbc");
    }
}