1. Method Overloading(메소드 오버로딩, 함수 오버로딩)

'과적'이라는 단어에 어울리게 함수 이름 하나로 다른 기능을 하는 함수를 만든다.

즉, 함수명이 같고, 함수 로직이 같다면 다른 것은 인풋 파라미터 뿐이다.

static int PlusMethod(int x, int y)
{
  return x + y;
}

static double PlusMethod(double x, double y)
{
  return x + y;
}

static void Main(string[] args)
{
  int myNum1 = PlusMethod(8, 5);
  double myNum2 = PlusMethod(4.3, 6.26);
  Console.WriteLine("Int: " + myNum1);
  Console.WriteLine("Double: " + myNum2);
}

-> 같은 함수명, 로직 & 다른 인풋 파라미터 타입 또는 개수

 

참고 : 같은 개념의 자바 코드

static int plusMethod(int x, int y) {
  return x + y;
}

static double plusMethod(double x, double y) {
  return x + y;
}

public static void main(String[] args) {
  int myNum1 = plusMethod(8, 5);
  double myNum2 = plusMethod(4.3, 6.26);
  System.out.println("int: " + myNum1);
  System.out.println("double: " + myNum2);
}

이래서 자바 짝퉁 소리 듣는거구나 싶다...

 

2. Method Overriding(메소드 오버라이딩, 함수 오버라이딩)

오버로딩과 헷갈릴 수 있는데... '최우선 되는'이라는 단어에 걸맞는 역할을 한다.

즉, 클래스를 상속받을 때, 상속받는 sub class(자식 클래스)에서 super class(부모 클래스)에 있는 '함수를 재정의'하는 것을 말한다. 이 때 재정의 된 메소드가 그 클래스 안에서 우선된다.

 

3. Polymorphism(다형성) 자바와의 차이

Java

class Animal {
  public void animalSound() {
    System.out.println("The animal makes a sound");
  }
}

class Pig extends Animal {
  public void animalSound() {
    System.out.println("The pig says: wee wee");
  }
}

class Dog extends Animal {
  public void animalSound() {
    System.out.println("The dog says: bow wow");
  }
}

class Main {
  public static void main(String[] args) {
    Animal myAnimal = new Animal();  // Create a Animal object
    Animal myPig = new Pig();  // Create a Pig object
    Animal myDog = new Dog();  // Create a Dog object
    myAnimal.animalSound();
    myPig.animalSound();
    myDog.animalSound();
  }
}


결과 :
The animal makes a sound
The pig says: wee wee
The dog says: bow wow

@Override annotation을 명시하지 않아도 기본적으로 method overriding이 된다.


C#

class Animal  // Base class (parent) 
{
  public void animalSound() 
  {
    Console.WriteLine("The animal makes a sound");
  }
}

class Pig : Animal  // Derived class (child) 
{
  public void animalSound() 
  {
    Console.WriteLine("The pig says: wee wee");
  }
}

class Dog : Animal  // Derived class (child) 
{
  public void animalSound() 
  {
    Console.WriteLine("The dog says: bow wow");
  }
}

class Program 
{
  static void Main(string[] args) 
  {
    Animal myAnimal = new Animal();  // Create a Animal object
    Animal myPig = new Pig();  // Create a Pig object
    Animal myDog = new Dog();  // Create a Dog object

    myAnimal.animalSound();
    myPig.animalSound();
    myDog.animalSound();
  }
}


결과 :
The animal makes a sound
The animal makes a sound
The animal makes a sound

기본적으로 Overriding이 되지 않는다.

 

class Animal  // Base class (parent) 
{
  public virtual void animalSound() 
  {
    Console.WriteLine("The animal makes a sound");
  }
}

class Pig : Animal  // Derived class (child) 
{
  public override void animalSound() 
  {
    Console.WriteLine("The pig says: wee wee");
  }
}

class Dog : Animal  // Derived class (child) 
{
  public override void animalSound() 
  {
    Console.WriteLine("The dog says: bow wow");
  }
}

class Program 
{
  static void Main(string[] args) 
  {
    Animal myAnimal = new Animal();  // Create a Animal object
    Animal myPig = new Pig();  // Create a Pig object
    Animal myDog = new Dog();  // Create a Dog object

    myAnimal.animalSound();
    myPig.animalSound();
    myDog.animalSound();
  }
}


결과 :
The animal makes a sound
The pig says: wee wee
The dog says: bow wow

override를 명시 해야지만 작동한다. 자바에서처럼 annotation으로 하지 않고 public override void 이런식으로 메소드 정의할 때 앞에 쓰는구나..

 

Tag. java overloading, java overriding, 자바 오버로딩, 자바 오버라이딩, 어노테이션, annotation, @override, @Override, 자바 다형성, 자바 polymorphism, java polymorphism, 자바 polymorphism, c# polymorphism, c sharp polymorphsim, 씨샵 polymorphism, c# 다형성, c sharp 다형성, 씨샵 다형성, 자바와 비교

+ Recent posts