ওভাররাইডিং (Overriding)
ওভাররাইডিং-এ, আপনি সাবক্লাস-স্পেসিফিক একটি বিহেভিয়ার ডিফাইন করতে পারেন, অর্থাৎ একটি সাবক্লাস পেরেন্ট ক্লাসের মেথডকে নিজের প্রয়োজন অনুযায়ী বাস্তবায়ন করতে পারে।
ওভাররাইডিং বাস্তবায়নকারী অ্যাবস্ট্র্যাক্ট ক্লাসের উদাহরণ নিচে দেওয়া হলো −
উদাহরণ
using System; namespace PolymorphismApplication { abstract class Shape { public abstract int area(); } class Rectangle: Shape { private int length; private int width; public Rectangle( int a = 0, int b = 0) { length = a; width = b; } public override int area () { Console.WriteLine("Rectangle class area :"); return (width * length); } } class RectangleTester { static void Main(string[] args) { Rectangle r = new Rectangle(10, 7); double a = r.area(); Console.WriteLine("Area: {0}",a); Console.ReadKey(); } } }মেথড হাইডিং (শাডোইং) (Method Hiding / Shadowing)
শাডোইং কে মেথড হাইডিংও বলা হয়। পেরেন্ট ক্লাসের মেথডটি চাইল্ড ক্লাসে override কীওয়ার্ড ছাড়া উপলব্ধ থাকে শাডোইং-এ। চাইল্ড ক্লাসের সেই ফাংশনের নিজস্ব ভার্সন থাকে।
শাডোইнг করতে new কীওয়ার্ড ব্যবহার করুন।
নিচে একটি উদাহরণ দেখি −
উদাহরণ
using System; using System.Collections.Generic; class Demo { public class Parent { public string Display() { return "Parent Class!"; } } public class Child : Parent { public new string Display() { return "Child Class!"; } } static void Main(String[] args) { Child child = new Child(); Console.WriteLine(child.Display()); } }