+92 332 4229 857 99ProjectIdeas@Gmail.com

Interfaces In Java


Interfaces In Java:
Java supports multiple interface inheritance. There are lots of interfaces that are provided by java such as ActionListener, KeyListener, WindowListener etc…

You can also define your interface as well. In interface you just declare your function names with prototypes only, not give their implementations, it is necessary for that class which is implementing this interface provide implementations of those methods that are declared in interface, you can also call interface as a pure abstract class. Remember, you can’t instantiate (making object of class) interface as well.

Code

For example:

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

public class GUIExample implements ActionListener {

JFrame myFrame = null;
       Container con = null;
JButton helloButton = null;

      public GUIExample () {
       
          createComponents();
      }

      public
void createComponents(){ // initializing GUI interface


              myFrame = new JFrame();
              helloButton = new JButton("Hello");
              con = myFrame.getContentPane();
              con.setLayout(new FlowLayout());
              con.add(helloButton);
              myFrame.setSize(250,100);
              myFrame.setVisible(true);
              myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             
// Registring event with helloButton
 helloButton.addActionListener(this); // "this" means this class

}

// If user press HelloButton then this event is invoked and display message
       public void actionPerformed(ActionEvent e) {

             if(e.getSource().equals(helloButton)) {

                JOptionPane.showMessageDialog(null, "HelloWorld");

              }
}
}

public static void main(String[] args) {

 GUIExample = new GUIExample(); // creating new object of GUIExample
       
        new GUIExample(); // creating new object of GUIExample (Second Way)
}


DEFINING YOUR OWN interface:

In the following code, defining your own interface , two methods display() and showResult() are with just their prototypes. Now its the headache of that class which is implementing this interface provide the implementation of those methods in the interface.


     public interface MyInterface {

       // can also define static final constants as well
       public static final int age = 10;
      
       // give the prototypes of your methods only
       public void display();
       public void showResult();
}

Here, you see TestInterface is implementing MyInterface and also providing proper implementation of display() and showResult() methods

public class TestInterface implements MyInterface {

public void display() {
          
           System.out.println("This is implementation of MyInterface display Method");
       }

public void showResult() {
          
          System.out.println("This is implementation of MyInterface showResult Method");
             
       }

       public static void main(String[] args) {
          
              new TestInterface().display();
              new TestInterface().showResult();
       }
 }

MULTIPLE interface INHERITANCE:

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

public class GUIExample implements ActionListener,KeyListener,WindowListener {
           
        JFrame myFrame = null;
        Container con = null;
        JButton helloButton = null;
        JTextField inputTextField = null;
             
      public GUIExample() {

         createComponents();
     }

      public void createComponents(){ //initializing GUI interface

              myFrame = new JFrame();
              helloButton = new JButton("Hello");
              inputTextField = new JTextField();
              inputTextField.setPreferredSize(new Dimension(70, 20));
              con = myFrame.getContentPane();
              con.setLayout(new FlowLayout());
             
              // Adding inputTextField and helloButton to container
              con.add(inputTextField);
              con.add(helloButton);
             
              myFrame.setSize(250,100);
              myFrame.setVisible(true);
              myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             
              // Registering event with helloButton , inputTextField and myFrame
              helloButton.addActionListener(this);
              inputTextField.addKeyListener(this);
              myFrame.addWindowListener(this);
                    
      }

        public void actionPerformed(ActionEvent e) {
    
           // if user press HelloButton then this message is displayed
                     if (e.getSource().equals(helloButton)) {

                           JOptionPane.showMessageDialog(null, "Hello Button Is Pressed");
                     }
              }

 // All Methods of KeyListener interface give implementaion of your required method otherwise leave empty bodies
       public void keyPressed(KeyEvent e) {

       // at every keypress in inputTextField this message display with the key pressed name
       JOptionPane.showMessageDialog(null, "You pressed " + e.getKeyText(e.getKeyCode()));

       }



       public void keyReleased(KeyEvent e){}
       public void keyTyped(KeyEvent e){}

// All Methods of WindowListener interface give implementaion of your required method otherwise leave empty bodies
       public void windowClosed(WindowEvent e) {}
       public void windowActivated(WindowEvent e){}   

       public void windowClosing(WindowEvent e) {

             // when user close the frame GoodBye!!!! message is displayed
              JOptionPane.showMessageDialog(null, "Good Bye!!!!");
       }

       public void windowDeactivated(WindowEvent e){}
       public void windowDeiconified(WindowEvent e){}
       public void windowIconified(WindowEvent e){}
       public void windowOpened(WindowEvent e){}
}

0 comments: