OOP ( Object Oriented Programming)
අපි අද බලමු මොනවද මේ OOP Concepts නැත්නම් Object Oriented Programming Concepts කියන්නේ කියලා සරලව තේරෙන්න code implementation නැතිව. ප්රධාන සංකල්ප හයකින් මේ OOP සමන්විත වෙන්වා.
- Class
- Object
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
බොහෝවිට මෙහි සදහන් Class & Object නොමැතිව ද මෙම OOP Concepts යන්න හදුන්වනු ලබනවා.
මුලින්ම අපි බලමු මොනවද මේ Object
Object එකක් කියන්නේ යම් කිසි දෙයක් යම්කිසි වස්තුවක් නැතහොත් දත්ත (data) සහ ඒ දත්තට අදාල ක්රියාවන් (actions ) එකට එකතු කරලා තියන ඒකකයක්.
උදාහරණය:
Get Navodya Dhanushka’s stories in your inbox
Join Medium for free to get updates from this writer.
ඔයාගේ වාහනය (vehicle) – එක object එකක් වගේ:
- දත්ත (data): brand එක, model එක, year එක
- ක්රියාවන් (actions): start කරන්න, drive කරන්න, brake එක apply කරන්න
දැන් අපි බලමු මොනවද මේ Classes කියන්නේ කියලා
Class එකක් කියන්නේ අපි කලින් කතා කරපු Object එකක් හදාගන්න භාවිතා කරන සැලැස්මක් ආකෘතියක් (Blueprint) වගේ එකක් ඒ කියන්නේ, class එකක තියෙන්නේ object එකකට තියෙන ගුණාංග (attributes / properties) සහ ක්රියාකාරකම් (methods / functions) නිවැරදිව විස්තර කරලා. ඒකෙන් object හදන්න පුළුවන්, ඒත් class එක තමයි හැඳින්වීමේ ආකෘතිය. Object එකක් කියන්නේ, ඒ class එකෙන් හදපු සජීවී උදාහරණය.
class Car {
String brand;
String color;
int year;
void start() {
System.out.println(brand + " is starting...");
}
void drive() {
System.out.println(brand + " is driving...");
}
}public class Main {
public static void main(String[] args) {
Car maruti = new Car();
maruti.brand = "Maruti";
maruti.color = "Red";
maruti.year = 2020;
maruti.start();
maruti.drive();
System.out.println();
Car audi = new Car();
audi.brand = "Audi";
audi.color = "Black";
audi.year = 2022;
audi.start();
audi.drive();
System.out.println();
Car bmw = new Car();
bmw.brand = "BMW";
bmw.color = "Blue";
bmw.year = 2021;
bmw.start();
bmw.drive();
}
}Encapsulation
Encapsulation කියන්නේ දත්ත හා ක්රියාකාරකම් නැතහොත් data & methods එකතු කර එක් ස්ථානයක class එකක් තුල සගවා තබා ගැනීමයි. දත්ත ආරක්ෂා කර ගැනීමට මෙය භාවිතා කරයි.
Abstraction
Abstraction කියන්නේ සාරාංශකරණය සංකීර්ණ භාවය අඩු කිරීම යන්නයි. ඒ කියන්නේ අවශ්ය දේවල් තොරතුරු පමණක් පෙන්වා අනවශ්ය දේවල් තොරතුරු සගවා තැබීමයි. මේ සදහා Abstract class හා Interfaces භාවිතා කරනවා.
Inheritance
මේ concept එකෙන් කියන්නේ සරලවම එක class එකක තියන දේවල් තවත් class එකකට පාවිච්චි කිරීමට අවස්ථාවක් දෙන එක. අලුත් class එකක් (child/subclass) පැරණි එකෙන් (parent/superclass) දේවල් අරගෙන වැඩිදුරටත් දියුණු කරනවා.
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Multiple Inheritance
- Hybrid Inheritance
Polymorphism
Polymorphism යනුවෙන් සදහන් කරන්නේ එකම සමාන නමකින් යුතු method එකක් විවිධ ආකාරයෙන් හැසිරෙන්න පුළුවන් බව. සරලවම එකම නම තියන method එකක් එක එක අවස්ථාවන් හී එක එක විවිධ වැඩ සිදු කිරීමයි.
- Method Overloading (එකම නම, වෙනස් parameters)
- Method Overriding (child class එකක් parent එකේ method එක වෙනස් කරනවා.
More Information









