Thursday, May 13, 2021

Java Programming sinhala tutorial - Interface - Part 30

 Interface

    අද අපි interface කියන්නේ මොකද්ද කියලා බලමු. Class එකක වගේම, interface එකකත් variables හා methods අපිට නිර්මාණය කරන්න පුළුවන්. නමුත් interface එකක තිබෙන methods, abstract methods වෙනවා (method signature එක පමණයි , body එකක් නැ ). 
    Interface එකකින් class එක මගින් කුමක්ද විය යුත්තේ කියලා දක්වන අතර කොහොමද කරන්නේ කියලා නොදක්වයි. 


  • Interface එකක් declare කරන්න "Interface" keyword එක පාවිච්චි කරනවා.
  • Interface එකක methods implicitly public හා abstract වේ.
  • Interface එකක variables implicitly public, static  හා final වේ.
  • Interface එකක් implement කිරීම සදහා "implement"  keyword එක පාවිච්චි කරනවා.
  • Interface හි declare කර ඇති සියලු methods එම interface එක implement කල class එකෙහි override කල යුතු වේ. එම methods public කල යුතු වේ.
  • Interface හි declare කර ඇති සියලු methods එම interface එක implement කල class එකෙහි override නොකරයි නම් එම class එක abstract විය යුතු වේ.

  • Interface type එකෙන් reference නිර්මාණය කල හැකි අතර object නිර්මාණය කල නොහැක. Interface එකට super class එකක් ලෙස ක්‍රියා කල හැකි අතර interface type reference එකට sub  class refer කල හැක.

  • Class එකකට වෙනත් class එකක් පමණක් extends කල හැකි අතර interface කිහිපයක් implements කල හැක.

  •  Interface එකකට වෙනත් interface එකක් හෝ කිහිපයක් extends කල හැක.

  • Interface සදහා constructor නිර්මාණය කල නොහැක.
  • JDK 8 මගින් default method හා static method නිර්මාණය කිරීමට හැකියාව ලබා දී ඇත. Default method මගින් interface එක තුල body එකක් සහිත method නිර්මාණය කිරීමට ඉඩ ලබා දෙයි.

  • JDK 9 මගින් private method, static method හා  private static method නිර්මාණය කිරීමට හැකියාව ලබා දී ඇත. 
  • Interface total abstraction, multiple inheritance හා loose coupling සදහා උපකාරී වේ.

අපි ඊලග ලිපියෙන් Java ගැන තවත් විස්තර ඉගෙන ගනිමු.

- චමෝදි -