Page 193 - CTS - CSA TP - Volume 2
P. 193
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 (InterfaceImplementationExample):
• Creates an instance of the Printer class.
• Calls the print method through the Printable interface.
Output:
In this example, the Printable interface declares an abstract method print. The Printer class implements this
interface and provides a concrete implementation for the print method. The main class demonstrates how to
create an instance of the implementing class and call the method through the interface.
Interfaces in Java are used to achieve abstraction, define a contract, and support multiple inheritances. Classes
can implement multiple interfaces, allowing for more flexibility in the design of Java programs.
TASK 2: Multiple Interfaces
// Interface 1
interface Printable {
void print();
}
// Interface 2
interface Showable {
void show();
}
// Class implementing multiple interfaces
class Display implements Printable, Showable {
@Override
public void print() {
System.out.println(“Printing...”);
}
@Override
public void show() {
178
CITS : IT & ITES - Computer Software Application - Exercise 113