Page 183 - CTS - CSA TP - Volume 2
P. 183

COMPUTER SOFTWARE APPLICATION - CITS




           EXERCISE 111 : Create interfaces in JAVA




            Objectives

           At the end of this exercise you shall be able to
           •  develop Java programs to Create interface in Java.

           Requirements


           Tools/Materials
           •  PC / laptop with windows OS
           •   SDK software
           •  Test editor (Visual studio/ subline/ notepad)

           Procedure


           TASK 1: Here’s a basic example of using an interface in Java:
              // Define an interface named Printable
              interface Printable {

                  void print(); // Abstract method without implementation
              }
              // Implement the Printable interface in a class
              class Printer implements Printable {

                  // Provide implementation for the print method
                  @Override
                  public void print() {
                      System.out.println(“Printing a document...”);
                  }

              }
              // Main class to demonstrate the interface usage
              public class InterfaceBasicExample {
                  public static void main(String[] args) {

                      // Create an instance of the Printer class
                      Printer myPrinter = new Printer();


                      // Call the print method through the Printable interface
                      myPrinter.print();
                  }

              }








                                                           168
   178   179   180   181   182   183   184   185   186   187   188