Abstract Vs Interface

23 Sept 2023

Abstract vs Interface

Abstract နဲ. interface ဘာကွာလဲလို. မေးခွန်းမျိုးမေးရင်။
Abstract class သည် zero or more abstract method ပါတယ်။သူ.ကို instantiate လုပ်လို.မရဘူး ။Non abstract method တွေလဲပါနိုင်တယ်။ Interface ကျတော့ အားလုံးက abstract method တွေစုထားတာ။
ဒါမျိုးဖြေကြတယ်။ မှန်သလားဆိုတော့ အဲ့တာက language ကသတ်မှတ်ထားတဲ့ rule, တကယ်တမ်း OO design အရ ဘယ်နေရာမှာတော့ abstract class သုံးရမယ် ဘယ်နေရာမှာ interface သုံးရမယ်ဆိုတာမျိုး သက်သက် ခွဲထုတ်လို.ရတယ်။

Interface သည် static typing language တွေမှာ duck typing မရတာကြောင့် ပေးထားရတာ။ Duck typing ဆိုတာ method name တူတယ် ဘယ် class ကကောင်ဆိုတာ သိစရာမလိုဘူး ဘယ် Object မှန်းသိစရာမလိုဘူး။ သူ. method ရှိရင်ပြီးရောဒါဆို ခေါ်ပေးမယ် ဒီသဘောကိုဆိုလိုတာ။

ဒါပေသိ static language တွေမှာ type တခုနဲ.တခုကို assign လုပ်ရင်ကို typing rule အရ base class ထဲကို child ထဲ့လို.ရမယ်။ Interface reference ထဲကို interface ကို implement လုပ်ထားတဲ့ class တွေရဲ object တွေရယ် သူ.ဆိုကို inherit လုပ်ထားတဲ့ကောင်တွေရယ် ထဲ့လို.ရမယ်ဆိုပြီး rule ထုတ်ထားလေ့ရှိတယ်။

အဲ့တော့ Interface သည် ဘုံမတူတဲ့ different family of object တွေကို method ဒါမှ မဟုတ် set of method တွေ ခေါ်ချင်ရင် သုံးရတယ်။ static language မှာ duck typing ကိုရဖို. Interface ကိုကြားခံသုံးရသလို.ပဲ။
ဒါက language rule အရ အဲ့လိုဖြစ်တာ။ ဒါဆို OO design အရဆိုရင်ရော abstract class တွေသည် common parent ဖြစ်တဲ့ကောင်တွေ child class တွေသည် common property တွေကို inherit လုပ်မယ်ဆိုရင် abstract class ကိုသုံးရမယ်။ Family တူတဲ့ Object တွေကို Polymorphism လုပ်ချင်ရင် Abstract ကိုသုံးရမယ်။ ဥပမာ Human သူ.မှာလဲ work ရှိတယ်။ Teacher မှာလဲ work ရှိမယ်။ Doctor မှာလဲရှိမယ်။ ဒါဆို Human ကို abstract class အနေနဲ.ထားရမယ်။ ဘာလို.လဲဆိုတော့ Teacher , Doctor သည် Human နဲ. sharing တူတယ်။ Hierarchy တူလို.။

Interface ကျတော့ design ပိုင်းအရဆို unrelated hierarchy ရှိတဲ့ class တွေကို Polymorphism လုပ်ချင်ရင်သုံးရမယ်။ ဥပမာ Bird မှာလဲ fly ရှိတယ်။ Areoplane မှာလဲ fly ရှိတယ်။ fly method ကို polymorphism လုပ်ချင်တယ်။ ဒါဆိုရင် ငှက်နဲ. လေယာဉ်ပျံသည် inhertiance hierarchy အရ တူတူထားလို.မရဘူး ဒါကြောင့် ဒီနေရာမှာ interface ပဲသုံးရမယ်။ ဆိုချင်တာက class တွေသည် sharing common ancestor မတူရင် polymorphism လိုရင် interface ကိုသုံးရမယ်လို.ဆိုချင်တာ။
နောက်တခုက abstract class ကိုသုံးရင် based class ကနေ child method တွေကို template , strategy pattern မှာသုံးသလို. base method တွေကနေ child method override method တွေကို လှမ်းခေါ်လို.ရမယ်။ တနည်းအားဖြင့် တချို. method တွေကို client ကိုပေး override လုပ်မယ် ဒါပေမဲ့ ကျောရိုးကိုတော့ framework , library တွေက ထိန်းမယ်ဆို ရင် abstract class ကိုသုံးရမယ်။ ဘာလို.လဲဆိုတော့ abstract class မှသာ parent to child method ကိုလှမ်းခေါ်လို.ရမှာကိုး ။ Interface ကျတော့ အဲ့လို parent to child, child to parent method ခေါ်လို.မရတော့ဘူး။
Inheritance ကိုစိတ်ဝင်စားရင် အောက် က paper ကိုဖတ်စေချင်တယ်။
https://www.csee.umbc.edu/…/reso…/papers/Inheritance.pdf
အဲ့ paper author က ဒေါက်တာဘွဲ.ကို Inheritance အကြောင်းရေးပြီး ရထားတာ။ Sun မှာတုန်းကတော့ VM တွေရေးတဲ့သူပေါ့။ စိတ်ဝင်စားရင် သူ. profile က ဒီမှာ http://www.cs.tut.fi/~taivalsa/

Original link

crd=>(https://m.facebook.com/story.php?story_fbid=pfbid02PCPbnfGYRFnXymApPwzeLA1FhFoNoek88SYNyvzVjDPbyacW7YjhkbmKPoGwaHnkl&id=1819241055&mibextid=Nif5oz)

Leave a comment