Wednesday, August 11, 2010

GUI සැකසීම සඳහා Basic Layouts යොදාගන්නා අයුරු

අපි මෙම පාඩමෙන් සාකච්ඡා කරන්න යන්නේ ජාවා වැඩසටහන් සඳහා අතුරුමුහුනත් සැකසීමේදී components සුදුසු පරිදි ස්ථානගත කිරීම සඳහා layouts යොදාගන්නා ආකාරයයි. මෙම layouts java.awt පැකේජයේ අන්තර්ගත වනවා. අප මෙම පාඩමේදී මූලික වශයෙන් පහත දැක්වෙන layouts භාවිතා කරන අයුරු සලකා බලමු.
  • FlowLayout
  • BorderLayout
  • GridLayout
අපි මුලින්ම බලමු ජාවා අතුරුමුහුණතකට Layout manager එකක් set කරන්නේ කොහොමද කියලා.
syntax:
void setLayout(LayoutManager layoutObj);
උදා:
this.setLayout(new FlowLayout());

දැන් අපි ඉහත සඳහන් කල layouts විස්තරාත්මකව සලකා බලමු

FlowLayout

Panel class එකෙහි සහ එහි සියලුම child classes (උදා:Applet,JApplet,JPannel,.. ) වල default layout එක වන්නේ මෙයයි. සාමාන්‍යයෙන් components place(ස්ථානගත) කරනුයේ වමේ සිට දකුණට සහ උඩ සිට පහලට යන ආකාරයෙනි(left to right and top to bottom).
constructor:
FlowLayout(int align, int hgap, int vgap);
align: මෙමගින් සඳහන් කරනුයේ components ස්ථානගත කරනුයේ මොන පැත්තෙන් ආරම්භ වන පරිදිද යන්නයි
FlowLayout.LEFT
FlowLayout.CENTER
FlowLayout.RIGHT
hgap, vgap : මෙමහින් සඳහන් කරනුයේ components අතර horizontal gap එක සහ vertical gap එකයි මෙය pixels වලින් ලබාදිය යුතුය.

දැන් අපි FlowLayout එක යොදාගෙන සරල  චිත්‍රකමුහුනතක් (GUI )සකසන අයුරු බලමු.
Code:
import java.awt.FlowLayout;
import javax.swing.*;

public class FlowLayoutDemo extends JFrame{

 public FlowLayoutDemo(){
  setTitle("FlowLayoutDemo Demo");  
  JPanel myPanel=new JPanel();
  myPanel.setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
  //creating JButton objects
  JButton btn1=new JButton("Component1");
  JButton btn2=new JButton("Component2");
  JButton btn3=new JButton("Component3");
  JButton btn4=new JButton("Component4");
  JButton btn5=new JButton("Component5");
  JButton btn6=new JButton("Component6");
  //adding JButton objects to the panel
  myPanel.add(btn1);
  myPanel.add(btn2);
  myPanel.add(btn3);
  myPanel.add(btn4);
  myPanel.add(btn5);
  myPanel.add(btn6);
  this.add(myPanel);
  this.setSize(492,100);
  this.setDefaultCloseOperation(3);
  this.setVisible(true);
 }

 public static void main(String args[]){
  new FlowLayoutDemo();
 }
}

BorderLayout

Window සහ එහි sub class සඳහා default layout එක වේ. මෙහිදී container එක කලාප 5 කට බෙදනු ලැබේ ඒවා නම්
  1. North
  2. South
  3. East
  4. West
  5. Center
components place කරනුයේ මෙම කලාප 5 ට වේ.
Constructor:
BorderLayout(int hgap, int vgap) 
hgap, vgap : මෙමහින් සඳහන් කරනුයේ components අතර horizontal gap එක සහ vertical gap එකයි මෙය pixels වලින් ලබාදිය යුතුය.

දැන් අපි BorderLayout එක යොදාගෙන සරල චිත්‍රකමුහුනතක් (GUI )සකසන අයුරු බලමු.
Code:
import java.awt.BorderLayout;
import javax.swing.*;

public class BorderLayoutDemo extends JFrame{

 public BorderLayoutDemo(){
  setTitle("BorderLayout Demo");  
  JPanel myPanel=new JPanel();
  myPanel.setLayout(new BorderLayout(5,5));
  //creating JButton objects
  JButton btn1=new JButton("North");
  JButton btn2=new JButton("East");
  JButton btn3=new JButton("South");
  JButton btn4=new JButton("West");
  JButton btn5=new JButton("Center");
  //adding JButton objects to the panel
  myPanel.add(btn1,BorderLayout.NORTH);
  myPanel.add(btn2,BorderLayout.EAST);
  myPanel.add(btn3,BorderLayout.SOUTH);
  myPanel.add(btn4,BorderLayout.WEST);
  myPanel.add(btn5,BorderLayout.CENTER);

  this.add(myPanel);
  this.pack();
  this.setDefaultCloseOperation(3);
  this.setVisible(true);
 }

 public static void main(String args[]){
  new BorderLayoutDemo();
 }
}
output:

GridLayout

මෙම layout එක බොහෝ දුරට FlowLayout එකට සමානය නමුත්  components place කරනුයේ සමාන cell sizes ඇති virtual grid එකක් තුලටය. මෙහිදී අදාල component එකෙහි preffered size එක නොසලකා හැරේ.

Constructor: 
GridLayout(int rows, int cols, int hgap, int vgap) ;

rows, cols : මෙමගින් දැක්වෙන්නේ components add කරනු ලබන virtual grid එකෙහි rows සහ columns ප්‍රමාණයයි.

hgap, vgap : මෙමහින් සඳහන් කරනුයේ components අතර horizontal gap එක සහ vertical gap එකයි මෙය pixels වලින් ලබාදිය යුතුය.
Code:

import java.awt.GridLayout;
import javax.swing.*;

public class GridLayoutDemo extends JFrame{

 public GridLayoutDemo(){
  setTitle("GridLayoutDemo Demo");  
  JPanel myPanel=new JPanel();
  myPanel.setLayout(new GridLayout(2,3,5,5));
  //creating JButton objects
  JButton btn1=new JButton("Component1");
  JButton btn2=new JButton("Component2");
  JButton btn3=new JButton("Component3");
  JButton btn4=new JButton("Component4");
  JButton btn5=new JButton("Component5");
  JButton btn6=new JButton("Component6");
  //adding JButton objects to the panel
  myPanel.add(btn1);
  myPanel.add(btn2);
  myPanel.add(btn3);
  myPanel.add(btn4);
  myPanel.add(btn5);
  myPanel.add(btn6);

  this.add(myPanel);
  this.pack();
  this.setDefaultCloseOperation(3);
  this.setVisible(true);
 }

 public static void main(String args[]){
  new GridLayoutDemo();
 }
}
output:

අපි ඉහත සාකච්ඡා කල layout managers කිහිපය GUI සෑදීමේදී බහුල වශයෙන් යෙදේ.

වඩාත් සංකීර්ණ GUI සෑදීම
මෙහිදී සිදු කෙරෙනුයේ Layouts කිහිපයක් සංයුක්ත කොට කොට භාවිතා කිරීමය. Panel objects කිහිපක් සුදුසු පරිදි යෙදවීමෙන් අපට වඩාත් සංකීර්ණ GUI සකසාගත හැකිය.
දැන් අපි Windows calculator එකෙහි අනුරුවක් අප ඉගෙනගත් LayoutManagers යොදාගෙන සෑදීමට උත්සාහ කරමු.
Output:
code:
import java.awt.*;
import javax.swing.*;

public class ComplexGUIDemo extends JFrame{

 public ComplexGUIDemo(){
  //this.setLayout(new GridLayout(3,1));
  this.setLayout(new FlowLayout(FlowLayout.RIGHT));
  //main panels
  JPanel txtPanel=new JPanel();
  JPanel numPanel1=new JPanel();
  JPanel numPanel2=new JPanel();
  //sub panels of numPanel2
  JPanel numPanel2Sub1=new JPanel();
  JPanel numPanel2Sub2=new JPanel();
  
  JTextField textBox=new JTextField("0",33);
  textBox.setHorizontalAlignment(JTextField.RIGHT);
  txtPanel.setLayout(new GridLayout(1,1));
  txtPanel.add(textBox);
  
  numPanel1.setLayout(new FlowLayout(FlowLayout.RIGHT));
  JButton btnbacksp=new JButton("Backspace");
  JButton btnce=new JButton("CE");
  JButton btnc=new JButton("C");
  numPanel1.add(btnbacksp);
  numPanel1.add(btnce);
  numPanel1.add(btnc);
  
  numPanel2Sub1.setLayout(new GridLayout(4,1,5,5));
  JButton btnMC=new JButton("MC");
  JButton btnMR=new JButton("MR");
  JButton btnMS=new JButton("MS");
  JButton btnMPl=new JButton("M+");
  numPanel2Sub1.add(btnMC);
  numPanel2Sub1.add(btnMR);
  numPanel2Sub1.add(btnMS);
  numPanel2Sub1.add(btnMPl);
  
  numPanel2Sub2.setLayout(new GridLayout(4,5,5,5));
  JButton btn7=new JButton("7");
  JButton btn8=new JButton("8");
  JButton btn9=new JButton("9");
  
  JButton btndv=new JButton("/");
  JButton btnsqrt=new JButton("sqrt");
  numPanel2Sub2.add(btn7);
  numPanel2Sub2.add(btn8);
  numPanel2Sub2.add(btn9);
  numPanel2Sub2.add(btndv);
  numPanel2Sub2.add(btnsqrt);
  
  JButton btn4=new JButton("4");
  JButton btn5=new JButton("5");
  JButton btn6=new JButton("6");
  
  JButton btnmul=new JButton("*");
  JButton btnperc=new JButton("%");
  numPanel2Sub2.add(btn4);
  numPanel2Sub2.add(btn5);
  numPanel2Sub2.add(btn6);
  numPanel2Sub2.add(btnmul);
  numPanel2Sub2.add(btnperc);
  
  JButton btn1=new JButton("1");
  JButton btn2=new JButton("2");
  JButton btn3=new JButton("3");
  
  JButton btnmin=new JButton("-");
  JButton btn1bx=new JButton("1/x");
  numPanel2Sub2.add(btn1);
  numPanel2Sub2.add(btn2);
  numPanel2Sub2.add(btn3);
  numPanel2Sub2.add(btnmin);
  numPanel2Sub2.add(btn1bx);
  
  JButton btn0=new JButton("0");
  JButton btnPM=new JButton("+/-");
  JButton btnpt=new JButton(".");
  
  JButton btnpl=new JButton("+");
  JButton btneq=new JButton("=");
  numPanel2Sub2.add(btn0);
  numPanel2Sub2.add(btnPM);
  numPanel2Sub2.add(btnpt);
  numPanel2Sub2.add(btnpl);
  numPanel2Sub2.add(btneq);
  
  numPanel2.setLayout(new FlowLayout());
  numPanel2.add(numPanel2Sub1);
  numPanel2.add(numPanel2Sub2);
  
  this.add(txtPanel);
  this.add(numPanel1);
  this.add(numPanel2);
  
  this.setTitle("My Calculator");
  this.setDefaultCloseOperation(3);
  this.setVisible(true);
  this.setSize(390,235);
 }

 public static void main(String args[]){
  new ComplexGUIDemo();
 }
}
GUI සම්බන්ද තවත් කරුණු අපි ඉදිරි පාඩම්වලදී සාකච්ඡා කරමු.
මෙම පාඩමට සම්බන්ද source codes බාගත කරගැනීම සඳහා සඳහා පහත ලින්ක් එක භාවිතා කරන්න. 

Saturday, July 31, 2010

Java වැඩසටහන් සඳහා User Interfaces සැකසීම :: Swing පැකේජය හැඳින්වීම III කොටස

JRadioButton
විකල්ප කිහිපයක් දී ඇති විටදී ඉන් එක් විකල්පයක් තෝරාගැනීමට අවකාශ සැලසීමට මෙම component එක යොදාගත හැක. උදාහරණයක් පහත ඉදිරිපත් කර ඇත. Mr,Mrs,Ms,Dr යන විකල්ප වලින් එකක් පමණක් තෝරාගැනීමට මෙහිදී සිදුවේ. මෙම පාඩමෙන් සිදුකෙරෙන්නේ JRadioButton එකක් සාදාගන්නා ආකාරය පමණි. මේවායේ event handling සිදුකිරීම ඉදිරි පාඩම් වලදී සාකච්ඡා කෙරේ.
JRadioButtonDemo.java
import javax.swing.*;
import java.awt.*;

public class JRadioButtonDemo extends JFrame{
 
 public JRadioButtonDemo(){
   setTitle("JRadioButton Demo");
   setLayout(new FlowLayout(FlowLayout.LEADING  ));
   setSize(300,300);
   JRadioButton jrb_mr=new JRadioButton("Mr");
   JRadioButton jrb_mrs=new JRadioButton("Mrs");
   JRadioButton jrb_ms=new JRadioButton("Ms");
   JRadioButton jrb_dr=new JRadioButton("Dr");
   //grouping
   ButtonGroup btnGroup=new ButtonGroup();
   btnGroup.add(jrb_mr);
   btnGroup.add(jrb_mrs);
   btnGroup.add(jrb_ms);
   btnGroup.add(jrb_dr);
   //add them to the JFrame
   add(jrb_mr);
   add(jrb_mrs);
   add(jrb_ms);
   add(jrb_dr);
   setVisible(true);
 }

 public static void main(String args[]){
  new JRadioButtonDemo();
 }
}
Output:

line14:
මෙහිදී අපි සාදාගත් JRadioButton ටික group කිරීම සිදුකෙරේ. මේවා group නොකලහොත් Mr, Mrs, Ms, Dr යන විකල්පයන්ගෙන් එකක් හෝ කිහිපයක් තේරීම සිදුකල හැකිය. නමුත් JRadioButton යෙදීමේ අරමුණ මෙය නොවන නිසා සාමාන්‍යයෙන් JRadioButtons group කිරීම සිදුවේ.

JList
කිසියම් elements ප්‍රමාණයක් පහත ආකාරයට දැක්වීමට JList නම් swing component එක භාවිතාකෙරේ.
උදා: JListDemo.java

import javax.swing.*;
import java.awt.*;

public class JListDemo extends JFrame{
 
 public JListDemo(){
   setTitle("JList Demo");
   setLayout(new FlowLayout(FlowLayout.LEADING  ));
   setSize(300,300);
   
   DefaultListModel dlm=new DefaultListModel();
   JList countries=new JList(dlm);
   dlm.addElement("Sri Lanka");
   dlm.addElement("India");
   dlm.addElement("Japan");
   dlm.addElement("Usa");
   dlm.addElement("UK");
   
   add(countries); 
   setVisible(true);
 }
   
   public static void main(String args[]){
  new JListDemo();
   } 
}
JList එක construct කරගැනීමට පෙර DefaultListModel එකක් සාදාගැනීම කල යුතුයි. පසුව අපි සාදාගත් JList එකට දත්ත add කිරීම්ට JList එක clear කිරීමට, JList එකෙන් elements ඉවත් කිරීම මෙම DefaultListModel object එක හරහා සිදුවේ.Line11 දී මෙම DefaultListModel object එකක් සාදාගෙන ඇත. 

Line12: මෙහිදී අපි සාදාගත් DefaultListModel object එක සහ JList එක සම්බන්ද කිරීම සිදුකෙරේ.

දැන් අපි JList එක හා සම්බන්ද වැදගත් method කිහිපයක් සලකා බලමු.

JList Methods
විස්තරය
int getSelectedIndex()  user විසින් list එකීහි මේ වනවිට select කර ඇති item එකෙහි index එක ලබාදේ.
void setSelectedIndex()  Program එක තුලින් list එකෙහි item එකක් select කර ඇති ලෙසට දක්වයි.
Object getSelectedValue()  user විසින් list එකීහි මේ වනවිට select කර ඇති item එක ලබාදේ
boolean isSelectedIndex(int idx)  ලබාදෙන int value එකක් select වී ඇති item එකේ index value එක නම් true ලෙසටද නැතිනම් false ලෙසටද return කරයි
setModel(ListModel model)  සකස් කරගත් ListModel object එකක් JList එක හා සම්බන්ද කිරීමට යොදාගැනේ
DefaultListModel Methods
විස්තරය
void add(int index, Object element)  ලබාදෙන index අගයකට(location එකකට) item එකක් ඇතුලු කිරීම සිදුකරයි
void addElement(Object obj)  list එකේ අගට item එකක් ඇතුලු කිරීම සිදුකරයි
int capacity()  දැනට list එකේ capacity එක නොහොත් items ගනණ ලබාදේ
void clear()  list එකේ ඇති සියලු items clear කිරීම සිදුකරයි.
boolean  contains(Object obj)  ලබදෙන Object එකක් list එක තුල අන්තර්ගත වේනම් true ලෙසද නැත්නම් false ලෙසටද ලබාදේ.
Object elementAt(int index)  ලබාදෙන index එකක ඇති value එක ලබාදේ
boolean isEmpty()  list එක empty නම් true ලෙසටද නැත්නම් false ලෙසටද ලබාදේ
Object remove(int index)  ලබාදෙන index එකක ඇති item එක ඉවත්කරයි
void removeAllElements()  list එක තුල ඇති සියලු items ඉවත් කරයි

JList එකට අදාල documentation එක කියවීම සඳහා මෙම ලින්ක් එකෙන් යන්න.
DefaultListModel එකට අදාල documentation එක කියවීම සඳහා මෙම ලින්ක් එකෙන් යන්න.

JList එකට scroll bar යෙදීමට අවශ්‍ය නම් JScrollPane එකක් යොදාගත හැකිය. එය සිදුකරන අයුරු පහත දැක්වේ.
import javax.swing.*;
import java.awt.*;

public class JListDemo extends JFrame{
 
 public JListDemo(){
   setTitle("JList Demo");
   setLayout(new FlowLayout(FlowLayout.LEADING  ));
   setSize(300,300);
   
   DefaultListModel dlm=new DefaultListModel();
   JList countries=new JList(dlm);  
   
   JScrollPane scrollP=new JScrollPane(countries);
   scrollP.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

   dlm.addElement("Sri Lanka");
   dlm.addElement("India");
   dlm.addElement("Japan");
   dlm.addElement("Usa");
   dlm.addElement("Canada");
   dlm.addElement("Australlia");
   dlm.addElement("Italy");
   dlm.addElement("South Affrica");
   dlm.addElement("Thailand");
   
   add(scrollP); 
   setVisible(true);
 }
   
   public static void main(String args[]){
  new JListDemo();
   } 
}
Output:
පහත ජාවා ඇප්ලට් එක භාවිතයෙන් ඔබට ඉහත සඳහන් කල methods හි ක්‍රියාකාරීත්වය පහසුවෙන් වටහාගත හැකිවෙතැයි සිතමි.
පාඩමේ භාවිතා කර ඇති මූලාශ්‍ර කේත(source codes) බාගත කරගැනීම සඳහා පහත ලින්ක් එක භාවිතා කරන්න.

Friday, July 23, 2010

Java වැඩසටහන් සඳහා User Interfaces සැකසීම :: Swing පැකේජය හැඳින්වීම II කොටස

අපි ඉහත කලින් පාඩමේදී සාකච්ඡා කලේ JLabel, JButton හා JTextField පිලිබඳවයි. මෙම පාඩමෙන් තවත් swing components කිහිපයක් පිලිබඳවා සාකච්ඡා කරමු.

JTextArea

මෙය JTextField සඳහා බොහෝ සමාන වුවද මෙහි lines කිහිපයකින් යුතු user inputs ගත හැක. එනම් address එකක් හෝ කිසියම් description එකක් ගැනීමේදී මෙය භාවිතා කල හැක.
JTextArea(int rows, int columns)
JTextArea(String text)
JTextArea(String text, int rows, int columns)
උදා;

අපිට අවශ්‍ය නම් මෙම JTextArea සඳහා scrollbar යෙදිය හැක. මේ සඳහා කල යුත්තේ JScrollPane එකක් තුලට JTextArea එක දැමීමයි. මෙය සිදුකරන අයුරු පහත දැක්වේ.
import javax.swing.*;
import java.awt.*;
class JTextAreaDemo extends JFrame{

 public JTextAreaDemo(){
  setTitle("JTextArea Demo");
  JTextArea jtxt=new JTextArea(10,20);
  JScrollPane scrollpane=new JScrollPane(jtxt);
  
  setLayout(new FlowLayout());
  setSize(300,300);
  setVisible(true);
  add(new JLabel("Description :"));
  add(scrollpane);
 }
 
 public static void main(String args[]){
  new JTextAreaDemo();
 }
}
Output:
JScrollPane එකෙහි constructor එකට(line 8) JTextArea object එක වන jtxt object එක ලබාදීම මගින් scrollpane එකට textarea එක ඇතුලුකිරීම සිදුකර ඇත.

JTextArea එකෙහි method ලෙස getText(),setText() බහුලව යෙදේ.


JCheckBox

විකල්ප 1ක් හෝ කීපයක් තේරීමට ඉඩලබාදිය යුතුවිට මෙය භාවිතා කෙරේ. 

isSelected()method එක භාවිතා කර යම් CheckBox එකක් select කර ඇත්දැයි පරීක්ෂා කල හැක. Event handling පාඩමේදී JCheckBox පිලිබඳ වැඩිදුරටත් සාකච්ඡා කෙරේ.
JCheckBox එකට අදාල documentation එක කියවීම සඳහා මෙම ලින්ක් එකෙන් යන්න.
 

Java වැඩසටහන් සඳහා User Interfaces සැකසීම :: Swing පැකේජය හැඳින්වීම I කොටස

අතුරු මුහුණත්(Graphical User Interfaces) යනු යම් පරිගණක වැඩසටහනක් භාවිතා කිරීම පහසු කරවන්නක්. සාමාන්‍ය පරිගණක පරිශීලකයෙක් නම් මේ වකවානුවේ බොහෝ දුරට භාවිතා කරනුයේ මෙවැනි වැඩසටහන්. ජාවා පරිගණක භාෂාව යොදාගෙන මෙවැනි GUI සහිත වැඩසටහන් සැකසීමට විශේෂ පහසුකම් ලබාදී තිබෙනවා. මේ සඳහා යොදාගත හැකි ප්‍රධාන ජාවා පැකේජ 2ක තමයි
  • AWT (Abstract Window Toolkit)
  • Swing
විශේෂ හේතූන් කිහිපයක් නිසා Swing පැකේජය භාවිතා කිරීම ප්‍රයෝජනවත් වනවා. ඉන් ප්‍රධානම හේතුව වන්නේ swing පැකේජය pure Java වලින් ලියැවුනු එකක් වීම. එම නිසා සුවිශේෂ වාසි කිහිපයක් අදාල වැඩසටහනට ලැබෙනවා. මීට අමතරව AWT සහය දක්වන සියලුම පහසුකම් සඳහා swing ද සහය දක්වනවා. AWT හි නොමැති පහසුකම් ගනණාවක්ද swing හි අඩංගු වනවා.
AWT සහ Swing හි වාසි අවාසි පිලිබඳ වැඩි විස්තර කියවීම සඳහා මෙම ලින්ක් එක භාවිතා කරන්න.

Friday, July 16, 2010

ජාවා interface සංකල්පය සහ එහි යෙදීම්

Interface එකක් කිව්වාම එකපාරටම මතක් වෙන්නෙ GUI එකක් නැත්නම් web form එකක් නේ. නමුත් මෙම පාඩමේදී අපි සලකා බලන්නේ වස්තු පාදක සංකල්පයක් වන interface සංකල්පය පිලිබඳවයි. මෙම සංකල්පයත් ජාවා ක්‍රමලේඛනයේ යෙදීමේදී බහුල වශයෙන් භාවිතා වන්නක්. මෙම පාඩමේදී පෙර පාඩම වන "ජාවා තුල Abstract Classes යොදාගන්නා අයුරු" යන පාඩමේදී උගත් කරුණු වැදගත් වන නිසා අවශ්‍ය නම් නැවත එය අධ්‍යයනය කරන්න.

interface එකක ඇති ප්‍රධාන ලක්ෂණ ගැන අපි සලකා බලමු.
  • interface එකක් යනු සම්පූර්ණයෙන්ම abstract class එකකි.

  • interface එකක ඇති methods සියල්ලම අර්ථදක්වා(define) ඇත්තේ method body එකකින් තොරවය(මෙයට හේතුව interface එක fully abstract වීමයි. එමනිසා මෙහි methods සියල්ලක්ම abstract වේ) 

  • interface එකක් තුල class variables (උදා : private int age;) පැවතිය නොහැක. නමුත් final attributes පැවතිය හැක(උදා: private final SIZE=512;)
මීලඟට අපි සලකා බලමු ලියන ලද interface එකක් භාවිතා කිරීමේදී සැලකිය යුතු කරුණු.
  • class එකකට අවශ්‍ය නම් interface එකකට වැඩි ප්‍රමාණයක් භාවිතා කල හැක. එනම් class එකකට අවශ්‍ය නම් interface එකකට වැඩි ප්‍රමාණයක් implement කල හැක. මේ සඳහා implement keyword එක භාවිතාවේ.

  • යම් class එකක් interface එකක් implement කර ඇත්නම් එම interface එකෙහි ඇති සියලුම methods override කිරීම අනිවාර්‍යයෙන්ම කල යුතුය.

  • class එක abstract නම් එසේ අවශ්‍ය නොවේ.

  • class වලදී මෙන් inheritance සංකල්පය interface සඳහාද භාවිතා කල හැක. එනම් interface එකකදී වෙනත් interface එකක් extend කල හැක.

  • multiple inheritance නම් සංකල්පය C++ වලදී මෙන් java තුලදී කිරීමට ඉඩදී නොමැති වුවද, මෙම interface සංකල්පය යොදාගෙන එම ක්‍රියාවලිය ඉටුකරගත හැක.

අපි දැන් බලමු interface එකක් ලිවීම සඳහා භාවිතා වන syntax එක
interface MyInterface {
 
 final int VAL=512;
 
 void myMethod1();
 int myMethod2();
 ......................
 ...................... 
}
දැන් අපි බලමු interface සංකල්පයේ යෙදීම් ආදර්ශනය කිරීමට ලියන ලද සරල වැඩසටහනක්
/**    
*class  : CarControllerDemo    
*Author : Kanishka Dilshan    
*Purpose: Describe interface concept in Java  
*Blog   : http://javaxclass.blogspot.com    
*/ 

interface CarController{
 final float MAX_SPEED=320f;
 
 void accelerate(); //accelerate the car
 void applyBreak(); //apply breaks
}

class BMW implements CarController{
 private String model;
 private float speed;
 
 public BMW(String model){
  this.model=model;
  speed=0f;
 }
 public void accelerate(){
 //implementing accelerate() method in the interface
  if(speed < MAX_SPEED)
   speed+=15;
 }
 
 public void applyBreak(){
 //implementing applyBreak() method in the interface
  if(!((speed-20) < 0))
   speed-=20;
  else
   speed=0;   
 }
 
 public void showDetails(){
  System.out.println("Car model : "+model);
  System.out.println("Speed     : "+speed);
 }
}

//another car called Nissan implements CarController interface
class Nissan implements CarController{
 private String model;
 private float speed;
 
 public Nissan(String model){
  this.model=model;
  speed=0f;
 }
 public void accelerate(){
 //implementing accelerate() method in the interface
  if(speed < MAX_SPEED)
   speed+=15;
 }
 
 public void applyBreak(){
 //implementing applyBreak() method in the interface
  if(!((speed-25) < 0))
   speed-=25;
  else
   speed=0;
 }
 
 public void showDetails(){
  System.out.println("Car model : "+model);
  System.out.println("Speed     : "+speed);
 }
}


public class CarControllerDemo{
 public static void main(String args[]){
  //creating BMW car object
  BMW car1=new BMW("BMW M6");
  Nissan car2=new Nissan("Altima Sedan");
  
  //accelerate car1 twice
  car1.accelerate();
  car1.accelerate();
  //applyBreak() for car1
  car1.applyBreak();
  car1.showDetails();
  
  //accelerate car2 4 times
  car2.accelerate();
  car2.accelerate();   
  car2.accelerate();
  car2.accelerate(); 
  //applyBreak() for car2
  car2.applyBreak();  
  car2.showDetails();
 }
}
Output:

පැහැදිලි කිරීම:
ඉහත උදාහරණයේ Line8 සිට Line13 දක්වා ඇත්තේ define කරන ලද interface එකයි. BMW සහ Nissan යන class තුලදී implement කර ඇත්තේ එම interface එක තුල ඇති accelerate() සහ applybreak() යන methods දෙකයි. මීට පෙර පාඩම් වලදී උගත් සිද්ධාන්ත භාවිතා කරමින් ඉහත කේතයේ අනෙකුත් කොටස් අවබෝධ කිරීම ඔබගේ කාර්‍යයකි. 

ජාවා ක්‍රමලේඛන භාෂාව යොදාගෙන වැඩසටහන් ලිවීමේදී පහත අවස්ථා වලදී මෙම interface භාවිතය ප්‍රායෝගිකව දැකගත හැකිය.
  • Event handling
  • Database connectivity
මෙම පාඩමත් සමග ජාවා වල මූලික OOP සංකල්ප කොටස අවසන් වේ. ඉදිරි පාඩම් වලදී ජාවහි අතුරු මුහුණත් සැකසීම පිලිබඳ අධ්‍යයනය කරමු.