Method hiding còn gọi là shadowing. Phương thức lớp cha có sẵn cho lớp con không dùng override. Lớp con có phiên bản riêng.
Dùng từ khóa new để shadowing.
Ví dụ
using System;
using System.Collections.Generic;
class Demo {
public class Parent {
public string GetInfo() {
return "Đây là lớp Parent!";
}
}
public class Child : Parent {
public new string GetInfo() {
return "Đây là lớp Child!";
}
}
static void Main(String[] args) {
Child child = new Child();
Console.WriteLine(child.GetInfo());
}
}
Kết quả
Đây là lớp Child!