OOP Basics Sinhala: Classes, Objects, Encapsulation Explained | Sri Lanka Dev Guide

💻 Object-Oriented Programming (OOP) වල මුල ඉඳන් ඉගෙන ගමු - කේත ලෝකේ පිළිවෙලට! 🚀
ආයුබෝවන් ඩෙවොලොපර් මචන්ලා සහ කේත ලෝකයට අලුතින් එන යාලුවනේ!
දැන් බලන්න, අපි පොඩි කාලේ ඉඳන්ම දේවල් පිළිවෙලට කරන්නනේ පුරුදු වෙලා තියෙන්නේ. අපේ පොත් ටික ටේබල් එකේ තියන විදිය, ඇඳුම් ටික කබඩ් එකේ ගහන විදිය, නැත්නම් අපේ හාමුදුරුවනේ, කුස්සියේ බඩු ටික තියන විදිය. මේ හැමදේම කරන්නේ අපිට ඕන වෙලාවට පහසුවෙන් හොයාගන්න පුළුවන් වෙන්නයි, වැඩේ ලේසි වෙන්නයිනේ. කෝඩිං ලෝකෙත් එහෙමයි මචන්ලා. අපේ කෝඩ් එක පොත් ගොඩක් වගේ අවුල් ජාලාවක් වෙනවා නම්, පස්සේ ඒක තේරුම් ගන්නත්, ඒක වෙනස් කරන්නත්, අලුත් දෙයක් එකතු කරන්නත් යන වෙලාවත්, මහන්සියත් කියා නිම කරන්න බෑ. අන්න ඒකට තමයි Object-Oriented Programming (OOP) කියන සුපිරි සංකල්පය තියෙන්නේ!
අද අපි කතා කරන්නේ OOP කියන්නේ මොකක්ද, ඒකෙන් අපේ වැඩේට වෙන හොඳ මොනවද, ඒකෙන් කෝඩ් කරනකොට පාවිච්චි වෙන Class, Object, Attributes, Methods වගේ මූලික සංකල්ප මොනවද කියලා. ඊට පස්සේ අපි පොඩි උදාහරණයක් එක්ක practical විදියටම බලමු මේවා කොහොමද කෝඩ් එකට දාගන්නේ කියලා. Ready නේද? එහෙනම් පටන් ගමු!
🤔 මොකක්ද මේ Object-Oriented Programming (OOP) කියන්නේ?
සරලවම කියනවා නම්, OOP කියන්නේ අපේ කෝඩ් එක ලියන, සංවිධානය කරන විදියක්. හිතන්නකෝ අපි කෝඩ් ලියනකොට, ඇත්ත ලෝකේ තියෙන දේවල් ගැන හිතලා, ඒ දේවල් වලට සමාන විදියට කෝඩ් එක ගොඩනගනවා කියලා. උදාහරණයක් විදියට, අපි Car එකක් ගැන හිතනවා නම්, ඒකට Color එකක්, Model එකක්, Brand එකක්, Year එකක් තියනවා. ඒ වගේම ඒකට Start කරන්න, Stop කරන්න, Accelerate කරන්න, Brake කරන්න වගේ දේවල් කරන්න පුළුවන් නේද? OOP වලදීත් අපි මේ වගේම දේවල් කරන්නේ. ඒ කියන්නේ, දත්ත (data) සහ ඒ දත්ත වලට කරන්න පුළුවන් දේවල් (actions) එකම තැනකට එකතු කරනවා.
මේකෙන් වෙන ලොකුම වාසිය තමයි, අපේ කෝඩ් එක පිළිවෙලකට, සංවිධානාත්මකව තියෙන එක. කෝඩ් එක ලොකු වෙනකොට, වෙනස් කරන්න ඕන වුණාම, නැත්නම් අලුත් Features එකතු කරන්න ඕන වුණාම, OOP පාවිච්චි කරලා තියෙනවා නම්, වැඩේ හරිම ලේසියි. ඒ වගේම, එක පාරක් හදපු කෝඩ් කොටසක් ආයේ ආයේ පාවිච්චි කරන්න පුළුවන් (reusability). ඒ කියන්නේ, කම්මැලි ඩෙවොලොපර්ලාට නියමයි!
🏗️ Classes (පංති) සහ Objects (වස්තූන්) - වෙනස මොකක්ද?
මෙන්න මේක තමයි OOP වලට එනකොට මුලින්ම තේරුම් ගන්න ඕනම දෙක. ගොඩක් අය මුලදී මේ දෙක පටලවා ගන්නවා. ඒත් මේක හරිම ලේසියි තේරුම් ගන්න.
Class (පංතිය)
Class එකක් කියන්නේ Blueprint එකක්, Template එකක්, නැත්නම් සැලැස්මක් වගේ. අපි හිතමුකෝ Car එකක් හදන කම්පැනියක් ගැන. ඒගොල්ලෝ එකම Model එකේ Car ගොඩක් හදනවා නේද? එතකොට ඒ Car එකේ Design එකක්, Blueprint එකක් තියෙනවා. ඒ Blueprint එකේ තියෙනවා Car එකට රෝද කීයක් තියෙන්න ඕනද, එන්ජිමේ Capacity එක කීයද, කීයටද පාට කරන්න ඕන වගේ දේවල්. ඒ කියන්නේ, ඒක අලුත් Car එකක් හදන්න තියෙන සැලැස්ම විතරයි. ඒ Blueprint එකෙන් ඇත්තටම Road එකේ දුවන්න බෑ නේද? ඒ වගේම තමයි Class එකක් කියන්නේ. ඒක ඇත්තටම දුවන දෙයක් නෙවෙයි, Object එකක් හදන්න තියෙන සැලැස්ම විතරයි.
class Car:
# මේක Car එකක Class එක
pass
Object (වස්තුව)
Object එකක් කියන්නේ Class එකකින් හදපු Actual Instance එකක්. අර Car එකේ Blueprint එකෙන් හදපු ඇත්තටම පාරේ දුවන Car එකක් වගේ. හිතන්න, Toyota Camry එකක්, Honda Civic එකක්, නැත්නම් Suzuki WagonR එකක්. මේ හැම එකක්ම Car කියන Class එකෙන් හදපු වෙන වෙනම Objects. හැම Object එකකටම තමන්ගේම Attributes (ගුණාංග) ටිකක් තියෙනවා (උදා: මගේ Camry එක රතු පාටයි, ඔයාගේ Civic එක නිල් පාටයි). ඒ වගේම, හැමෝටම Start කරන්න, Stop කරන්න වගේ Methods (ක්රම) ටිකක් කරන්නත් පුළුවන්.
my_car = Car() # මේක Car Class එකෙන් හදපු Object එකක්
your_car = Car() # මේකත් තව Object එකක්
ඉතින් සාරාංශයක් විදියට, Class එක Blueprint එක, Object එක ඒ Blueprint එකෙන් හදපු Actual product එක. තේරුණා නේද මචන්?
✨ Attributes (ගුණාංග) සහ Methods (ක්රම) - වස්තුවක 'මොනවද' සහ 'මොනවද කරන්නේ'?
අපි දැන් Class එකක් සහ Object එකක් කියන්නේ මොනවද කියලා දන්නවා. දැන් බලමු Object එකක තියෙන Attributes සහ Methods කියන්නේ මොනවද කියලා.
Attributes (ගුණාංග)
Attributes කියන්නේ Object එකක තියෙන Properties නැත්නම් Characteristics. අර Car උදාහරණයෙන් කියනවා නම්, Car එකක Brand එක (Toyota), Model එක (Camry), Color එක (Red), Year එක (2020) වගේ දේවල් තමයි Attributes කියන්නේ. මේවා Object එකේ තත්වය (state) ගබඩා කරලා තියනවා.
class Car:
def __init__(self, brand, model, color, year):
self.brand = brand
self.model = model
self.color = color
self.year = year
my_car = Car("Toyota", "Camry", "Red", 2020)
print(my_car.color) # Output: Red
Methods (ක්රම)
Methods කියන්නේ Object එකකට කරන්න පුළුවන් Actions නැත්නම් Behaviors. Car එකක Start කරන එක, Stop කරන එක, Accelerate කරන එක, Brake කරන එක වගේ දේවල් තමයි Methods කියන්නේ. මේවා Object එකේ තත්වය වෙනස් කරන්න නැත්නම් ඒ තත්වය පාවිච්චි කරලා යම්කිසි දෙයක් කරන්න පාවිච්චි වෙනවා. Methods ඇත්තටම Class එකක් ඇතුළේ define කරලා තියෙන functions.
class Car:
def __init__(self, brand, model, color, year):
self.brand = brand
self.model = model
self.color = color
self.year = year
def start(self):
print(f"The {self.color} {self.brand} {self.model} is starting...")
def stop(self):
print(f"The {self.brand} {self.model} is stopping.")
my_car = Car("Toyota", "Camry", "Red", 2020)
my_car.start() # Output: The Red Toyota Camry is starting...
my_car.stop() # Output: The Toyota Camry is stopping.
🔒 Encapsulation (ආවරණය) - රහස් ටික රහසින් තියමු!
අපි සාමාන්ය ජීවිතේදී සමහර දේවල් රහසින් තියනවා නේද? නැත්නම් හැමෝටම පෙන්නන්නේ නෑ. උදාහරණයක් විදියට, අපේ ෆෝන් එකේ ඇතුලේ තියෙන Hardware components ගැන අපිට දැනගන්න ඕනේ නෑ ඒක වැඩ කරන්නේ කොහොමද කියලා. අපිට ඕනේ ෆෝන් එක වැඩ කරන එක විතරයි. Call ගන්න, SMS යවන්න, Apps පාවිච්චි කරන්න පුළුවන් නම් ඇති. ඇතුලේ circuits කොහොමද සම්බන්ධ වෙන්නේ කියලා අපි දැනගන්න ඕනේ නෑ. එතකොට ඒක හදපු කෙනා (Manufacturer) ඒ details ටික අපිෙන් හංගලා තියෙනවා.
Encapsulation කියන්නෙත් ඒ වගේම දෙයක්. OOP වලදී, Encapsulation කියන්නේ Object එකක තියෙන Data (Attributes) සහ ඒ Data වලට අදාළව වැඩ කරන Methods ටික එකම Unit එකක් විදියට එකතු කරන එක. ඒ වගේම, සමහර අභ්යන්තර විස්තර (internal details) පිටින් ඉන්න අයට (outside world) පෙන්නන්නේ නැතුව හංගලා තියන එක. මේකෙන් වෙන්නේ මොකක්ද දන්නවද? Object එකක Data එකට කෙලින්ම ගිහින් වෙනස් කරන්න (direct access) බෑ. එහෙම කරන්න ඕනේ නම් Methods හරහා විතරයි කරන්න පුළුවන්.
මේකෙන් වෙන ලොකුම වාසි දෙක තමයි:
- Data Security: අනවශ්ය විදියට අපේ Data වෙනස් වීම වළක්වනවා.
- Flexibility: Class එකක අභ්යන්තර ක්රියාකාරීත්වය (implementation) වෙනස් කළත්, ඒ Class එක පාවිච්චි කරන අනිත් කෝඩ් වලට ඒක බලපාන්නේ නෑ. මොකද එළියට පේන Interface එක (Methods) වෙනස් නොකරන නිසා.
class BankAccount:
def __init__(self, account_number, balance):
self.__account_number = account_number # Private attribute
self.__balance = balance # Private attribute
def get_balance(self):
return self.__balance
def deposit(self, amount):
if amount > 0:
self.__balance += amount
print(f"Deposited {amount}. New balance: {self.__balance}")
else:
print("Deposit amount must be positive.")
def withdraw(self, amount):
if 0 < amount <= self.__balance:
self.__balance -= amount
print(f"Withdrew {amount}. New balance: {self.__balance}")
else:
print("Invalid withdrawal amount or insufficient funds.")
my_account = BankAccount("123456789", 1000)
# print(my_account.__balance) # Error! Cannot directly access private attribute
my_account.deposit(500) # Valid way to change balance
my_account.withdraw(200) # Valid way to change balance
my_account.withdraw(2000) # Invalid withdrawal
print(my_account.get_balance()) # Access balance via a method
පයිතන් වලදී, Attribute එකක් Private (හංගනවා) කරන්න ඒකේ නමේ මුලට Double Underscore (__
) දානවා. ඒක සම්පූර්ණයෙන්ම හංගන්නේ නෑ. ඒත් පිටින් ඉන්න කෙනෙක්ට කෙලින්ම access කරන්න දෙන්නේ නෑ. ඒක Python වල Naming convention එකක් විතරයි (Name Mangling).
🛠️ ප්රායෝගික උදාහරණයක් - Car Class එකක් හදමු!
දැන් අපි කතා කරපු හැමදෙයක්ම එකට එකතු කරලා පොඩි Python කෝඩ් කෑල්ලක් ලියමු.
පියවර 1: Class එකක් Define කරමු
මුලින්ම අපි Car
කියන Class එක define කරමු. මේක තමයි අපේ Blueprint එක.
class Car:
pass # දැනට මොකුත් නෑ, Class එක විතරයි
පියවර 2: Constructor එකක් (__init__
) සහ Attributes එකතු කරමු
__init__
කියන්නේ Class එකකින් Object එකක් හදන හැම වෙලාවකම automatic execute වෙන Method එක. මේකට Constructor කියලත් කියනවා. මේක ඇතුලේ අපි අපේ Object එකට ඕන කරන මුලික Attributes ටික set කරනවා.
class Car:
def __init__(self, brand, model, color, year, current_speed=0):
# Constructor method එක. Object එකක් හැදෙනකොට මේක execute වෙනවා.
self.brand = brand # Car එකේ Brand එක (e.g., Toyota)
self.model = model # Car එකේ Model එක (e.g., Camry)
self.color = color # Car එකේ Color එක (e.g., Red)
self.year = year # Car එක හදපු අවුරුද්ද (e.g., 2020)
self.current_speed = current_speed # Car එකේ Speed එක. Default 0.
self.is_engine_on = False # එන්ජිම off ද on ද කියලා
# 'self' කියන්නේ මොකක්ද?
# 'self' කියන්නේ Object එකම identify කරන reference එකක්.
# අපි Method එකක් හෝ Attribute එකක් Object එකට අයිතියි කියලා කියන්නේ 'self' පාවිච්චි කරලා.
# Python වලදී Method එකක පලවෙනි parameter එක 'self' වෙන්නම ඕනේ. අනිත් programming languages වල 'this' වගේ.
self
කියන එක ගැන පොඩ්ඩක් සැලකිලිමත් වෙන්න. ඒක තමයි ඔයාගේ මේ Car
Object එකට අදාළව වැඩ කරන්න අවශ්ය reference එක දෙන්නේ. ඒක නැතුව මේ Methods වලට ඔයාගේ Car
එකේ brand
එක මොකක්ද, model
එක මොකක්ද කියලා දැනගන්න බෑ.
පියවර 3: Methods එකතු කරමු
දැන් අපි අපේ Car එකට කරන්න පුළුවන් දේවල් (behaviors) ටික Methods විදියට එකතු කරමු.
class Car:
def __init__(self, brand, model, color, year, current_speed=0):
self.brand = brand
self.model = model
self.color = color
self.year = year
self.current_speed = current_speed
self.is_engine_on = False
def start_engine(self):
if not self.is_engine_on:
self.is_engine_on = True
print(f"The {self.color} {self.brand} {self.model}'s engine is ON. Vroom vroom!")
else:
print(f"The {self.brand} {self.model}'s engine is already ON.")
def stop_engine(self):
if self.is_engine_on:
self.is_engine_on = False
self.current_speed = 0 # එන්ජිම off කරනකොට speed එක 0 වෙනවා
print(f"The {self.brand} {self.model}'s engine is OFF. Goodbye!")
else:
print(f"The {self.brand} {self.model}'s engine is already OFF.")
def accelerate(self, speed_increase):
if self.is_engine_on and speed_increase > 0:
self.current_speed += speed_increase
print(f"Accelerating! Current speed: {self.current_speed} km/h.")
elif not self.is_engine_on:
print("Cannot accelerate, engine is OFF.")
else:
print("Speed increase must be positive.")
def brake(self, speed_decrease):
if self.is_engine_on and speed_decrease > 0:
self.current_speed = max(0, self.current_speed - speed_decrease)
print(f"Braking! Current speed: {self.current_speed} km/h.")
elif not self.is_engine_on:
print("Cannot brake, engine is OFF.")
else:
print("Speed decrease must be positive.")
def display_info(self):
print("\n--- Car Information ---")
print(f"Brand: {self.brand}")
print(f"Model: {self.model}")
print(f"Color: {self.color}")
print(f"Year: {self.year}")
print(f"Current Speed: {self.current_speed} km/h")
print(f"Engine Status: {'ON' if self.is_engine_on else 'OFF'}")
print("-----------------------")
පියවර 4: Objects හදලා වැඩ කරමු!
දැන් අපේ Car Class එක ලෑස්තියි. අපි මේකෙන් Objects හදලා වැඩ කරන්න පටන් ගමු.
# Car Class එකෙන් Objects හදමු
my_sedan = Car("Toyota", "Corolla", "Silver", 2022)
my_suv = Car("Honda", "CRV", "Blue", 2020)
# Objects වල Attributes access කරමු
print(f"My sedan is a {my_sedan.color} {my_sedan.brand} {my_sedan.model}.")
print(f"Your SUV is a {my_suv.color} {my_suv.brand} {my_suv.model}.")
# Objects වල Methods call කරමු
my_sedan.display_info()
my_sedan.start_engine()
my_sedan.accelerate(50)
my_sedan.brake(20)
my_sedan.display_info()
my_sedan.stop_engine()
my_sedan.display_info()
print("\nNow for the SUV:")
my_suv.display_info()
my_suv.start_engine()
my_suv.accelerate(70)
my_suv.brake(30)
my_suv.stop_engine()
my_suv.display_info()
# තව Object එකක් හදමු
my_electric_car = Car("Tesla", "Model 3", "White", 2023)
my_electric_car.display_info()
my_electric_car.start_engine()
my_electric_car.accelerate(100)
my_electric_car.stop_engine()
ඔන්න ඔහොමයි අපි Class එකක් හදලා, ඒකෙන් Objects හදලා, ඒ Objects වල තියෙන Attributes සහ Methods පාවිච්චි කරන්නේ. මේක ඔයාගේ Programming journey එකේ තියෙන ලොකුම milestone එකක්.
🚨 පොදු වැරදි සහ හොඳ පුරුදු (Common Mistakes and Best Practices)
OOP පටන් ගන්නකොට සමහර පොදු වැරදි තියෙනවා. ඒවා ගැනත් පොඩ්ඩක් බලලා යමු.
AttributeError
මෙන්න මේ Error එක ඔයාලට ගොඩක් වෙලාවට එන්න පුළුවන්. මේක එන්නේ ඔයාලා Object එකක නැති Attribute එකක් call කරන්න ගියොත්. උදාහරණයක් විදියට, my_car.model
වෙනුවට my_car.modle
කියලා ටයිප් කරොත් වගේ. ඒ නිසා හරියට නම ටයිප් කරලා තියෙනවද කියලා බලන්න.
my_car = Car("Toyota", "Corolla", "Silver", 2022)
# print(my_car.modle) # This will cause an AttributeError!
Understanding self
(self තේරුම් ගැනීම)
අපි කලින් කතා කළා වගේ, self
කියන්නේ Python වල Class එකක Method එකක් ඇතුළේ ඒ Object එකම reference කරන එකට. අනිත් Programming languages වල this
කියන keyword එකට සමානයි. මේක නැතුව Method එකකට තමන්ට අයිති Attributes access කරන්න බෑ. අනිවාර්යයෙන්ම Method එකක පලවෙනි Parameter එක self
වෙන්න ඕනේ.
# වැරදි විදිය: self parameter එක නැතිනම්
# class MyClass:
# def my_method(): # Error! self parameter is missing
# print("Hello")
# නිවැරදි විදිය:
class MyClass:
def my_method(self):
print("Hello")
Naming Conventions (නම් කිරීමේ සම්මුති)
කෝඩ් ලියනකොට නම දැමීමේ (Naming) හොඳ පුරුදු පාවිච්චි කරන එක හරිම වැදගත්. මේකෙන් ඔයාගේ කෝඩ් එක කියවන්න සහ තේරුම් ගන්න හරිම ලේසියි.
- Classes: Class නම් කරනකොට PascalCase (Capital letters for the first letter of each word) පාවිච්චි කරන්න. උදා:
Car
,BankAccount
,UserProfile
. - Attributes and Methods: Attributes සහ Methods නම් කරනකොට snake_case (All lowercase, words separated by underscores) පාවිච්චි කරන්න. උදා:
my_variable
,get_balance()
,calculate_total()
.
🎉 නිගමනය
ඔන්න ඉතින් මචන්ලා, අපි Object-Oriented Programming (OOP) වල මූලික සංකල්ප ටික හොදටම කතා කළා. Class, Object, Attributes, Methods, සහ Encapsulation කියන්නේ මොනවද කියලා දැන් ඔයාලට පැහැදිලියි නේද? මේවා තමයි OOP වල කොඳු නාරටිය. මේ සංකල්ප හරියට තේරුම් ගත්තා නම්, ඔයාලට ඕනම ලොකු project එකක කෝඩ් එක හරිම පිළිවෙලකට, maintain කරන්න ලේසි වෙන විදියට ලියන්න පුළුවන් වෙනවා. ඒ වගේම, මේවාට අදාළව තව Polymorphism, Inheritance වගේ දේවලුත් තියෙනවා, ඒ ගැන අපි ඊළඟ ලිපියකදී කතා කරමු.
මේවා සරලව තියෙනවා වගේ පෙනුනත්, ඇත්තටම තේරුම් ගන්න පොඩි කාලයක් යයි. ඒත් practice කරනකොට මේක නිකන්ම auto වෙනවා. ඉතින්, අදම ඔයාලා කැමති පොඩි Class එකක් හදලා බලන්න. උදාහරණයක් විදියට Dog
Class එකක් හදලා, ඒකට name
, breed
වගේ Attributes දාලා, bark()
, eat()
වගේ Methods ටිකක් එකතු කරලා බලන්න. එතකොට තව දුරටත් මේ concepts ඔලුවට වදිනවා.
මේ ලිපිය ගැන ඔයාලගේ අදහස්, ප්රශ්න මොනවද? පහලින් comment section එකේ කියන්න. අපි කතා කරමු. තවත් මේ වගේ වැදගත් ලිපියකින් හමුවෙමු. කෝඩිං වැඩ සාර්ථක වේවා! 🚀