කොහොමද යාලුවනේ ! ඔන්න අදත් මං ආවා oriented object oriented concept එක තියන අලුත්ම පාඩමක් සමගින්. පුරුදු විදිහටම object oriented පාඩම් මාලාවේ තෙවැනි පාඩමත් සමග එක්වන ඔයාලා ඔක්කොම සාදරයෙන් පිළිගන්නවා.කලින් පාඩම් සමග ඔයාලා සම්බන්ධ වුණා නම් දැන් දන්නවා ඇති මොකක්ද object oriented concept කියන්නේ, class එකක් සහ object එකක් අපි හදා ගන්නේ කොහොමද කියල සහ access modifiers කියන්නේ මොකක්ද කියලා. තුන්වෙනි පාඩම පටන් ගන්න ඉස්සරින් access modifiers ගැන පොඩ්ඩක් මතක් කරන්නම්.ඇත්තටම access modifier එකකින් කරන්නේ තමන්ගේ class එකට සහ class එක ඇතුලේ තියන attributes වල්ට හා methods වල්ට ආරක්ෂාව සපයන එක.
ඔන්න ඕක තමයි access modifiers ගැන තියන සාරාංශය. access modifiers ගැන ඔයාලට අමතක වෙලා තිබුන නම් දැන් මතක් වෙන්න ඇති කියලා හිතනවා.හරි එහෙනම් අපි බලමු අද පාඩම මොකක්ද කියලා. අද අපි වැඩිපුර methods ගැන කතා තමයි කරන්න බලාපොරොත්තු වෙන්නේ.ඔයාලා දැනටමත් දන්නවා මොකක්ද method එකක් කියන්නේ කියලා .තවදුරටත් අපි methods ගැන කියනවනම්, methods එකක් නම් ඒකට return type එකක් තියෙන්න ඕන. return type එක කියන්නේ methods එකෙන් අපට මොන වර්ගයේ value එකක් ද එළියට ගන්න ඕනි කියන එක. උදාහරණයක් විදිහට ගන්නවනම් අපට යම්කිසි method එකක් පාවිච්චි කරලා යම්කිසි ඉලක්කමක් method එකෙන් එළියට ගන්න ඕනි නම් අපිට පාවිච්චි කරන්න පුළුවන් int, double හෝ float වගේ data type එකක්. මොකක් හරි වචනයක් අපිට මෙතන දෙකෙන් එළියට ගන්න ඕනි නම් අපිට පාවිච්චි කරන්න පුළුවන් string කියන data type එක. කිසිම value එකක් අපිට method එකෙන් එළියට ගන්න ඕනි නැතිනම් අපිට void කියන return type එක පාවිච්චි කරන්න පුළුවන්. ඔන්න ඕකට තමයි return type එක කියලා කියන්නේ. මීට අමතරව method එකක් නම් ඒකට අපි කලින් කතා කරපු access modifier එකක් තතියෙන්නත් ඕනි. අපි කලින් කළා වගේ method එකකට parameter එකක් හෝ parameters කිහිපයක් assign කරලා අපිට method එක ඇතුලට values pass කරන්නත් පුළුවන්.
ඔන්න ඔය විදිහට තමයි method එකක් හදන්නේ. ඔන්න ඔය method එකේ නමත් එක්ක වරහන් ඇතුලෙ තියන parameters සෙට් එකට අපි කියනවා signature එක කියලා return type එක signature විදිහට අපි ගන්නෙ නෑ.
දැන් අපි කතා කරන්න යන්නේ දැන් අපි කතා කරන්න යන්නේ methods වල තියෙන විශේෂ අවස්ථා දෙකක් ගැන. ඔයා හිතන්න ඔයා ඔයාගේ class එකේ නමින් methods දෙකක් හෝ කිහිපයක් තියෙනවා කියලා. ඔයා දැන් object එකක් හදලා එක method එකක් call කළොත් compiler එකට හිතාගන්න බැරිවෙනවා කොයි method එකද call කරන්නේ කියල. අන්න එතකොට ඔයාගේ program එකේ Error එකක් පෙන්නනවා. අන්න ඒක අපිට වලක්වා ගන්න පුළුවන් ඔයාගේ method එකේ වරහන් ඇතුලේ තියෙන parameters ගණන හරි ඒ වගේ data type එක හරි වෙනස් කරලා .පසුව ඔයාට පුළුවන් ඔයාට අවශ්ය method එක call කරවන්න. ඔන්න ඕකට අපි කියනවා method overloading කියලා. method overloading කියන්නේ object oriented concept එක තියන ඉතා විශේෂ දෙයක්. method overloading ටිකක් තේරෙනවා මදි වගෙ නම් අපි උදා උදාහරණයක් බලමු.
හරි මම හදනවා class එකක් හැඩතල වල වර්ගඵලය සොයන්න. හැඩතල විදිහට මම ගන්නවා සමචතුරස්රය වෘත්තය හා ඍජුකෝණාස්රය. සමචතුරස්රයක වර්ගඵලය සොයන්න නම් එකට value එකක් හොඳටෝම ඇති. වෘත්තයකත් එහෙමමයි. සෘජුකෝණාස්රයක නම් දිග හා පළල කියලා values දෙකක් දෙන්න වෙනවා. මම හදන class එකේ method එකේ නම විදිහට මම ගන්නවා area කියලා. method overloading නිසා හැඩතල සියල්ලගෙම වර්ගඵලය සෙවීම්ට දන්නා method වල නම් aria කියන එකම විය යුතුයි. දැන් අපිට මේ method එකිනෙකින් වෙන්කර ගැනීමට නම් parameter signature එක වෙනස් කරන්න වෙනවා කියලා ඔයාලට තේරෙනවා ඇති. සෘජුකෝණාස්රයක වර්ගඵලය සෙවීම්ට parameters දෙකක් අවශ්ය නිසා ඒ method එක අනික් method දෙකෙන් වෙන්කර ගන්න පුළුවන්. අනික් method දෙක එකිනෙකින් වෙන්කර ගැනීම්ට මම වෘත්තයක වර්ගඵලය සොයන method එකේ parameter එකේ data type එක float ලෙසද අනෙක සඳහා integer අගයක්ද ලබා දෙනවා. දැන් ඔබට තේරෙනවා ඇති අනෙක් method දෙකෙත් parameter signature එක වෙනස් කියලා. ඔන්න ඔය විදිහට එකම නම තියන method වල parameters signature එක වෙනස් කරල තමයි අපි method overloading කරන්නේ.හරි එහෙනම් දැන් අපි අපේ වර්ගඵලය සොයන class එක හදමු.
class Area{ public void area(float x){ float c=3.14*x*x; System.out.print("aria of circle is :"+c);} public void area(int x){ int A=x*x; System.out.print("aria of square is:"+A);} public void area(int x,int y){ int A=x*y; System.out.print("aria of rectangle is :"+A);} } class CalArea{ public static void main(String a[]){ Area A1=new Area(); // if you want to calculate area of circle,pass a float value as an argument.. A1.area(5.2); // if you want to calculate area of square ,pass an integer value as an argument.. A1.area(5); // if you want to calculate area of rectangle, pass two integers as the arguments.. A1.area(5,10); } }
ඔන්න ඔහොම තමයි method overloading කරන්නේ. අද කරපු පාඩම තේරුනා කියලා හිතනවා. method overloading එපමණයි. method overriding සහ constructor එක ගැන මීළඟ පාඩමෙන් කියලා දෙන්නම්.ස්තුතියි !
4 Comments
who cares · March 19, 2018 at 7:40 pm
තද ඒ්ක දන්ත තද
Gayan Sampath · March 20, 2018 at 1:56 pm
chik
Ireshan · March 21, 2018 at 4:27 pm
Lesson was very clear and useful to me. But I didn’t understand what is the significance of the method overloading? What is the difference between name all methods with “area” vs name such methods as circle_Area etc ?
P.S . Method overloading became as “Method overriding” in few places.
Gayan Sampath · March 21, 2018 at 5:50 pm
method overloading means creating two or more methods using same method name whose changing parameter signature .compiler can understand which method is called?,because each method has unique parameter signature.that is the method overloading means.i hope that you will understand.if you have any another doubt about oop concept contact me.
i have used “method overriding” in few places.that is my mistake.I apologize you .i will correct those places right now. thank you!