Java Classes and Objects - OOP මූලික concepts | SC Guide

ආයුබෝවන් කට්ටිය! කොහොමද ඉතින් ඔයාලට? අද අපි කතා කරන්න යන්නේ software engineering වලට අත්යවශ්යම, මූලිකම concept එකක් ගැන – ඒ තමයි Classes සහ Objects.
ඔයාලා කවදාවත් හිතල තියෙනවද අපි පාවිච්චි කරන phone එකේ ඉඳන්, bank system එකක් වෙනකම් මෙච්චර complex software කොහොමද හදන්නේ කියලා? ඒ හැම එකක් පිටිපස්සෙම තියෙන ප්රධානම ක්රමය තමයි Object-Oriented Programming (OOP). අද අපි මේ OOP ලෝකයට අතපොවලා බලමු, විශේෂයෙන්ම Java programming language එක ඇතුලේ මේ Classes හා Objects කියන්නේ මොනවද කියලා.
හිතන්න, ඔයා ගෙයක් හදනවා කියලා. ගෙයක් හදන්න කලින් අපිට ඕනේ මොකක්ද? Plan එකක්, blueprint එකක් නේ? ඒ blueprint එක තමයි Class එකක් කියන්නේ. ඒ blueprint එකෙන් හදන ගෙවල් ටික තමයි Objects. හරිම සරලයි නේ? අපි මේක තවදුරටත් ගැඹුරින් බලමු.
OOP කියන්නේ මොකක්ද? (What is OOP?)
සරලවම කිව්වොත්, OOP කියන්නේ program එකක් design කරන විදිහක්. මේකේදී අපි real-world entities වගේම concepts program එක ඇතුළට ගේනවා. උදාහරණයක් විදිහට, මනුස්සයෙක්, කාර් එකක්, බැංකු ගිණුමක් වගේ දේවල් අපිට program එකකදී objects විදිහට නිරූපණය කරන්න පුළුවන්.
OOP වල ප්රධාන වාසි කිහිපයක් තියෙනවා:
- Modularity (ඛණ්ඩනය): Program එකේ කොටස් වෙන් වෙන්ව තියාගන්න පුළුවන්. ඒකෙන් code එක organize කරන්න ලේසියි.
- Reusability (නැවත භාවිතය): එක පාරක් හදපු code එක නැවත නැවතත් පාවිච්චි කරන්න පුළුවන්. අලුතෙන් ලියන්න ඕනේ නැහැ.
- Maintainability (නඩත්තුව): Code එකේ අඩුපාඩු හදන්න, අලුත් features එකතු කරන්න ලේසියි.
- Scalability (ව්යාප්ත කිරීම): Program එකක් ලොකු කරන්න (expand) ඕනෙ වෙලාවට පහසුයි.
මේ වාසි නිසා තමයි අදටත් ලොකුම software projects වලට OOP පාවිච්චි කරන්නේ.
Class එකක් කියන්නේ මොකක්ද? (What is a Class?)
කලින් කිව්වා වගේ, Class එකක් කියන්නේ blueprint එකක්. නැත්නම් template එකක්. මේකෙන් තමයි අපි Objects හදන්නේ. Class එකක ප්රධාන කොටස් දෙකක් තියෙනවා:
- Attributes (ගුණාංග): මේවා තමයි object එකක තියෙන දත්ත. උදාහරණයක් විදිහට, Car Class එකක "make", "model", "year" වගේ දේවල් attributes වෙන්න පුළුවන්. මේවා variables විදිහට තමයි define කරන්නේ.
- Behaviors (හැසිරීම්): මේවා තමයි object එකකට කරන්න පුළුවන් දේවල්. උදාහරණයක් විදිහට, Car Class එකක "start()", "stop()", "drive()" වගේ දේවල් behaviors වෙන්න පුළුවන්. මේවා methods විදිහට තමයි define කරන්නේ.
Java වලදී Class එකක් ලියන්නේ මෙහෙමයි:
public class Car {
// Attributes (ගුණාංග)
String make;
String model;
int year;
String color;
// Behaviors (හැසිරීම්) - Methods
public void start() {
System.out.println(make + " " + model + " starting...");
}
public void stop() {
System.out.println(make + " " + model + " stopping.");
}
public void drive(int speed) {
System.out.println(make + " " + model + " is driving at " + speed + " km/h.");
}
}
මෙහිදී, Car
කියන්නේ Class එකේ නම. make
, model
, year
, color
කියන්නේ මේ Car
Class එකේ Attributes. start()
, stop()
, drive()
කියන්නේ Behaviors (methods).
Object එකක් කියන්නේ මොකක්ද? (What is an Object?)
Class එකක් blueprint එකක් නම්, Object එකක් කියන්නේ ඒ blueprint එකෙන් හදපු actual instance එක. ඒ කියන්නේ, Car
Class එකෙන් අපිට actual cars හදන්න පුළුවන්. ඒ හැම Car එකක්ම වෙන වෙනම Object එකක්. හැම Object එකකටම තමන්ගේම Attributes වලට අදාළ values තියෙනවා.
Java වලදී Object එකක් හදන්නේ new
keyword එක පාවිච්චි කරලා:
public class Main {
public static void main(String[] args) {
// Creating an Object (instance) of the Car class
Car myCar = new Car();
// Setting attributes for myCar object
myCar.make = "Toyota";
myCar.model = "Aqua";
myCar.year = 2018;
myCar.color = "Blue";
// Calling behaviors (methods) on myCar object
System.out.println("My Car: " + myCar.color + " " + myCar.year + " " + myCar.make + " " + myCar.model);
myCar.start();
myCar.drive(60);
myCar.stop();
System.out.println("\n--- Another Car ---");
// Creating another Object of the Car class
Car yourCar = new Car();
yourCar.make = "Honda";
yourCar.model = "Vezel";
yourCar.year = 2020;
yourCar.color = "Red";
System.out.println("Your Car: " + yourCar.color + " " + yourCar.year + " " + yourCar.make + " " + yourCar.model);
yourCar.start();
yourCar.drive(80);
yourCar.stop();
}
}
මේ උදාහරණයේදී myCar
සහ yourCar
කියන්නේ Car
Class එකෙන් හදපු Objects දෙකක්. මේ Object දෙකටම make
, model
, year
, color
වගේ තමන්ටම ආවේණික values තියෙනවා. ඒ වගේම start()
, stop()
, drive()
වගේ behaviors තමන්ගේ specific data එකත් එක්ක execute කරන්න පුළුවන්.
Attributes සහ Behaviors ගැඹුරින් (Attributes and Behaviors in Depth)
අපි මේ Attributes හා Behaviors කියන දේවල් තව පොඩ්ඩක් පැහැදිලි කරගමු. මොකද මේවා තමයි OOP වල heart එක.
Attributes (State)
Attributes කියන්නේ object එකක state එක. ඒ කියන්නේ ඒ වෙලාවේදී object එක ගැන තියෙන තොරතුරු. උදාහරණයක් විදිහට, අපි Dog
කියලා Class එකක් ගත්තොත්, ඒ Dog
Class එකේ Attributes වෙන්න පුළුවන්:
String name;
(බල්ලාගේ නම)String breed;
(ජාතිය)int age;
(වයස)String color;
(පාට)
Object එකක් හදනකොට මේ Attributes වලට values දෙනවා. උදාහරණයක් විදිහට myDog
කියලා object එකක් හැදුවොත්, myDog.name = "Max";
, myDog.breed = "Labrador";
වගේ values දෙන්න පුළුවන්. Max
කියන බල්ලාට තමන්ගේම වයසක්, පාටක් වගේම තියෙන්න පුළුවන්.
Behaviors (Actions)
Behaviors කියන්නේ object එකකට කරන්න පුළුවන් actions. මේවා Methods විදිහට තමයි ලියන්නේ. Dog
Class එකේ Behaviors වෙන්න පුළුවන්:
public void bark() { System.out.println("Woof! Woof!"); }
(බුරන එක)public void eat(String food) { System.out.println("Eating " + food); }
(කන එක)public void sleep() { System.out.println("Zzzzzz..."); }
(නිදාගන්න එක)
අපි myDog
object එක හදලා ඒකේ bark()
method එක call කලොත්, myDog.bark();
කියලා ලියන්න පුළුවන්. එතකොට "Woof! Woof!"
කියලා print වෙයි. eat("bone")
කියලා call කලොත් "Eating bone"
කියලා print වෙයි.
මේ විදිහට තමයි real-world entities program එකක් ඇතුළට ගේන්නේ. එක Class එකක් විවිධ Objects ගොඩක් හදන්න පුළුවන්, ඒ හැම Object එකක්ම තමන්ගේම Attributes values රඳවාගෙන, Behaviors execute කරනවා.
වැදගත්කම සහ ඊළඟ පියවර (Importance and Next Steps)
හිතන්න ඔයාට university students ලා 1000 කගේ විස්තර manage කරන්න software එකක් හදන්න ඕනේ කියලා. Classses හා Objects නැතුව මේක කරනවා නම්, හැම student කෙනෙක්ටම වෙන වෙනම variables දහස් ගණනක් හදන්න වෙයි (name1, age1, major1, name2, age2, major2...). මේක මාරම අමාරු වැඩක්.
ඒත් Class එකක් පාවිච්චි කරනකොට, ඔයාට Student
කියලා Class එකක් හදලා, ඒකේ name
, age
, major
වගේ Attributes දාලා, enrollCourse()
, calculateGPA()
වගේ Behaviors දාන්න පුළුවන්. ඊට පස්සේ student 1000 කට new Student()
කියලා Objects 1000ක් හදලා, ඒ හැම object එකටම තමන්ගේම values දෙන්න පුළුවන්. මේකෙන් code එක organize වෙනවා වගේම, manage කරන්නත් පුදුමාකාර විදිහට ලේසි වෙනවා.
මේ Classes හා Objects කියන්නේ OOP වල මූලිකම concepts. මේවා හොඳින් තේරුම් ගත්තොත්, encapsulation, inheritance, polymorphism වගේ අනිත් advanced OOP concepts තේරුම් ගන්න එක හරිම ලේසියි. ඒ වගේම, ඕනෑම programming language එකක OOP concepts apply කරන්න පුළුවන්.
ඉතින්, අද අපි කතා කරපු දේවල් ඔයාලට පැහැදිලි වෙන්න ඇති කියලා හිතනවා. දැන්ම ඔයාලගේ computer එකේ Java IDE (IntelliJ IDEA, Eclipse, VS Code වගේ එකක්) එකක් open කරලා, අපි කතා කරපු Class එකයි Object එකයි හදලා වැඩ කරනවද කියලා බලන්න. ඒක තමයි best way එක මේවා තේරුම් ගන්න.
මේ post එක ගැන ඔයාලගේ අදහස් පහලින් comment කරන්න. තව මොන වගේ topics ගැනද ඔයාලට දැනගන්න ඕනේ කියලත් කියන්න. අපි ඊළඟ post එකෙන් හම්බවෙමු! තෙරුවන් සරණයි!