Page 195 - CTS - CSA TP - Volume 2
P. 195
COMPUTER SOFTWARE APPLICATION - CITS
EXERCISE 114 : Extend interfaces in JAVA
Objectives
At the end of this exercise you shall be able to
• develop Java programs to extend interface in Java.
Requirements
Tools/Materials
• PC / laptop with windows OS
• SDK software
• Test editor (Visual studio/ subline/ notepad)
Procedure
TASK 1: Extending Interfaces
// Base interface
interface Shape {
void draw(); // Abstract method
}
// Extended interface inheriting from Shape
interface Colorable extends Shape {
void color(); // Additional abstract method
}
// Class implementing the extended interface
class Square implements Colorable {
@Override
public void draw() {
System.out.println(“Drawing a square”);
}
@Override
public void color() {
System.out.println(“Coloring the square”);
}
}
// Main class
public class InterfaceExtensionExample {
180