Page 184 - CTS - CSA TP - Volume 2
P. 184
COMPUTER SOFTWARE APPLICATION - CITS
Explanation:
1 Interface (Printable):
• Declares a single abstract method named print().
2. Class Implementing the Interface (Printer):
• Implements the Printable interface.
• Provides a concrete implementation for the print method.
3. Main Class (InterfaceBasicExample):
• Creates an instance of the Printer class.
• Calls the print method through the Printable interface.
This basic example demonstrates how an interface defines a contract (in this case, the print method), and a class
implementing the interface must provide a concrete implementation for that method. The main class then utilizes
the interface to call the implemented method. The use of interfaces allows for a level of abstraction and helps
achieve better code organization and maintainability.
Output:
TASK 2 : In Java, an interface is a collection of abstract methods. It provides a way to achieve
abstraction and multiple inheritance. Here is an example of creating interfaces in Java
// Example 1: Basic Interface
interface Printable {
void print(); // Abstract method (no method body)
}
// Example 2: Interface with Constant
interface Shape {
double PI = 3.14; // Constant (implicitly public, static, and final)
double calculateArea(); // Abstract method
}
// Example 3: Interface with Default Method
interface Greeting {
void greet(); // Abstract method
default void farewell() {
System.out.println(“Goodbye!”); // Default method with implementation
169
CITS : IT & ITES - Computer Software Application - Exercise 111