[C#] 상속과 가상함수, 추상함수

< 상속 >

아래 예시에 Human 클래스와 Student 클래스가 있다. 

 

1. 상속 방법은 클래스 이름 옆에 " : 상속할 클래스"와 같이 적어주면 된다. 아래 예시에선 Student클래스가 Human클래스를 상속했다.

2. 상속을 했다면 이제 Student클래스에선 Human 클래스의 변수와 함수를 사용할 수 있다. 물론 Human클래스에서 private로 쓰인 변수나 함수는 사용하지 못한다.

 

 

Human.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Human : MonoBehaviour
{

    protected string humanName;
    protected int humanAge;

    protected void Info()
	{
        print("나는 인간입니다.");	
    }
}

 

Student.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Student : Human
{
    private string schoolName;

    void Start()
    {
        schoolName = "aa 초등학교";
        humanName = "aaa";
        humanAge = 10;

        Info();
    }

}

 

 

< 가상 함수(virtual function)와 재정의(override) >

부모 클래스의 함수를 자식 클래스에서 재정의(override)하여 사용하고 싶을 때 가상 함수를 사용할 수 있다. 예를들면 위의 Human클래스의 Info()함수를 자식 클래스인 Student클래스에선 "나는 학생입니다"라는 말도 출력하고 싶다면 아래와 같이 가상 함수를 사용하고 자식 클래스에서 재정의한다.

 

 

Human.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Human : MonoBehaviour
{

    protected string humanName;
    protected int humanAge;

    protected virtual void Info()
	{
        print("나는 인간입니다.");	
    }
}

 

Student.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Student : Human
{
    private string schoolName;

    void Start()
    {
        schoolName = "aa 초등학교";
        humanName = "aaa";
        humanAge = 10;

        Info();
    }


	protected override void Info()
	{
		base.Info(); // base는 부모 클래스를 가리킴
        print("나는 학생입니다.");
	}
}

 

< 추상 함수(abstract function)와 재정의(override) >

자식 클래스에서 무조건! 어떤 함수를 구현하라고 부모 클래스에서 명시하는 방법이 추상 함수이다. 코드가 수백, 수천줄이 되면 필요한 함수가 구현되지 않았다는 걸 파악하지 못할 수 있기 때문에 까먹지 않고 구현해야 할 때 사용할 수 있다. 부모 클래스의 추상 함수는 미완성인 함수이고 함수가 미완성이므로 클래스도 미완성이라 abstract class라고 적어주어야 한다.

 

Human.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class Human : MonoBehaviour
{

    protected string humanName;
    protected int humanAge;

    protected virtual void Info()
	{
        print("나는 인간입니다.");	
    }

    abstract protected void Name();
}

 

Student.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Student : Human
{
    private string schoolName;

    void Start()
    {
        schoolName = "aa 초등학교";
        humanName = "aaa";
        humanAge = 10;

        Info();
    }


	protected override void Info()
	{
		base.Info(); // base는 부모 클래스를 가리킴
        print("나는 학생입니다.");
	}

	protected override void Name()
	{
        print(humanName);
	}
}

 


 

< 정리 >

가상함수 : 부모 클래스에서 함수를 완성 시켰지만 자식 클래스에서 추가적으로 편집 가능한 함수

추상함수 : 부모 클래스에서 함수가 미완성이고 자식 클래스에서 무조건 그 함수를  완성시켜야하는 함수. 그리고 class에도 abstract를 써주야함.

 

위의 기능을 통해 다형성을 가질 수 있게 한다.

미리 토대를 만들어줌 = 다형성

'CS > C#' 카테고리의 다른 글

[C#] Thread  (2) 2025.08.13
[C#] readonly  (1) 2025.07.21
[C#] static, 싱글톤  (0) 2023.05.03
[C#] 델리게이트(Delegate)와 이벤트(Event)  (0) 2022.07.28
[C#] 컬렉션(Collection)과 제네릭 컬렉션(Generic Collection)  (0) 2022.07.26