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

COMPUTER SOFTWARE APPLICATION - CITS



               public void performTask() {
                   System.out.println(“Performing a task...”);

               }
               // Implementation of Flyable interface method
               @Override
               public void fly() {
                   System.out.println(“Flying...”);

               }
               // Implementation of Swimmable interface method
               @Override
               public void swim() {

                   System.out.println(“Swimming...”);
               }
               // Main method
               public static void main(String[] args) {
                   InterfaceExample example = new InterfaceExample();
                   // Calling methods from implemented interfaces

                   example.print();
                   System.out.println(“Area: “ + example.calculateArea());
                   example.greet();
                   example.farewell(); // Calling default method

                   example.performTask();
                   example.fly();
                   example.swim();
               }
           }

           Explanation:
           1  Basic Interface (Printable):
              •  Declares a single abstract method print().
           2  Interface with Constant (Shape):

              •  Declares a constant PI and an abstract method calculateArea().
           3  Interface with Default Method (Greeting):
              •  Declares an abstract method greet() and a default method farewell() with an implementation.
           4  Interface with Static Method (Utility):
              •  Declares an abstract method performTask() and a static method showInfo() with an implementation.
           5  Interface Inheritance (Flyable and Swimmable):

              •  Two interfaces with different methods.
              •  A third interface (FlyingSwimmingCreature) inherits from both Flyable and Swimmable.
           6.  Main Class (InterfaceExample):




                                                           171
 CITS : IT & ITES - Computer Software Application - Exercise 111  CITS : IT & ITES - Computer Software Application - Exercise 111
   181   182   183   184   185   186   187   188   189   190   191