Page 192 - CTS - CSA TP - Volume 2
P. 192
COMPUTER SOFTWARE APPLICATION - CITS
EXERCISE 113 : Create and implement an interface
Objectives
At the end of this exercise you shall be able to
• develop Java programs to Create and implement an interface.
Requirements
Tools/Materials
• PC / laptop with windows OS
• SDK software
• Test editor (Visual studio/ subline/ notepad)
Procedure
TASK 1: Interface and Implementation
// 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(“Welcom to Java”);
}
}
// Main class to demonstrate interface implementation
public class InterfaceImplementationExample {
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();
}
}
177
CITS : IT & ITES - Computer Software Application - Exercise 112