Java 8 Default Methods: Interface වල අලුත් Methods! - SC Guide

මොකද යාළුවනේ, කොහොමද? Java 8 ආවට පස්සේ Interfaces වලට හොඳටම සැර වැඩි වුණා කියලා ආරංචියක් ආවනේ. ඇත්තටම මොකද වුණේ? කලින් තිබ්බ Interface concept එකට වඩා Java 8 මේක තවත් powerful කරේ කොහොමද? අද අපි කතා කරමු Java 8 වල Interface වලට අලුතින් ආපු default methods සහ static methods ගැන. මේවා මොකටද, කොහොමද පාවිච්චි කරන්නේ කියලා අපි විස්තරාත්මකව බලමු.
ඔබ Java වල Object-Oriented Programming (OOP) concepts ඉගෙනගන්නකොට Interfaces ගැන අහලා ඇති. Interfaces කියන්නේ abstract methods විතරක් තියෙන (Java 8ට කලින්), ඒ වගේම static final variables තියෙන blueprint එකක් වගේ දෙයක්. ඒත් මේකේ පොඩි ප්රශ්නයක් තිබුණා. අපි බලමු මොකක්ද ඒ කියලා.
Interfaces වලට මොකද වුණේ? (What happened to Interfaces?)
Java 8 එන්න කලින් Interface එකකට පුළුවන් වුණේ abstract methods විතරක් තියාගන්න. මේ abstract methods කියන්නේ implementation එකක් නැති method signature එකක් විතරයි. මේවා implementation කරන්න ඕනේ ඒ interface එක implement කරන class එකක් ඇතුළේ. මේකෙන් interface එක use කරන class එකට 'මේ methods ටික implement කරන්නම ඕනේ' කියලා rule එකක් දැම්මා.
දැන් හිතන්න, අපි දැනටමත් විශාල project එකක් කරනවා කියලා. ඒකේ Interface එකක් තියෙනවා, ඒක class ගොඩකට implement කරලා තියෙනවා. හිතමු, Vehicle
කියලා Interface එකක් තියෙනවා, ඒක Car
, Bike
, Truck
වගේ classes ගොඩකට implement කරලා කියලා.
public interface Vehicle {
void start();
void stop();
}
public class Car implements Vehicle {
@Override
public void start() {
System.out.println("Car started.");
}
@Override
public void stop() {
System.out.println("Car stopped.");
}
}
// Imagine many other classes like Bike, Truck implementing Vehicle
දැන් අපිට Vehicle
interface එකට අලුතින් method එකක් honk()
කියලා add කරන්න ඕනේ වුණා කියලා හිතමු. කලින් තිබ්බ විදිහට අපි Vehicle
interface එකට void honk();
කියලා add කරොත්, හපොයි! ඒ interface එක implement කරලා තියෙන සියලුම classes (Car
, Bike
, Truck
, වගේ හැම එකම) compile errors පෙන්නනවා, මොකද ඒ අලුත් method එක implement කරලා නැති නිසා.
මේක ලොකු ප්රශ්නයක්. මොකද, interface එකකට පොඩි වෙනසක් කරොත්, project එකේ ගොඩක් තැන්වල වෙනස්කම් කරන්න වෙනවා. මේ ප්රශ්නයට තමයි Java 8 වලින් default methods කියන සංකල්පය ගෙනාවේ. ආයෙත් වැඩේ ලේසි වුණා!
Default Methods: Interfaces වලට අලුත් ජීවයක් (New Life for Interfaces)
Default methods කියන්නේ Interface එකක් ඇතුළේ implementation එකක් එක්ක ලියන්න පුළුවන් methods. මේවා default
keyword එකෙන් define කරනවා. මේකෙන් වෙන්නේ, Interface එකට අලුත් method එකක් add කරාම, ඒ method එකට default implementation එකක් Interface එක ඇතුළෙන්ම දෙන්න පුළුවන් වීම. ඒ නිසා, ඒ Interface එක implement කරලා තියෙන classes වලට ඒ method එක අනිවාර්යයෙන්ම implement කරන්න අවශ්ය වෙන්නේ නෑ.
නමුත්, අවශ්ය නම්, ඒ classes වලට මේ default method එක override කරන්නත් පුළුවන්. නියම වැඩේ!
උදාහරණයක් බලමු (Let's look at an example):
public interface Drivable {
void drive(); // Abstract method - must be implemented
default void startEngine() {
System.out.println("Engine started. Vroom vroom!");
}
default void stopEngine() {
System.out.println("Engine stopped.");
}
}
public class Car implements Drivable {
@Override
public void drive() {
System.out.println("Car is driving gracefully.");
}
// Car can use the default startEngine() and stopEngine() methods
// or override them if needed
@Override
public void startEngine() {
System.out.println("Car engine starts with a roar!");
}
}
public class Bicycle implements Drivable {
@Override
public void drive() {
System.out.println("Bicycle is pedaling along.");
}
// Bicycle doesn't have an engine, so it overrides the default method
@Override
public void startEngine() {
System.out.println("Bicycle doesn't have an engine. Just pedal faster!");
}
// It might also override stopEngine() or use the default
}
public class DefaultMethodDemo {
public static void main(String[] args) {
Car myCar = new Car();
myCar.drive();
myCar.startEngine(); // Calls overridden method
myCar.stopEngine(); // Calls default method
System.out.println("\n---");
Bicycle myBicycle = new Bicycle();
myBicycle.drive();
myBicycle.startEngine(); // Calls overridden method
myBicycle.stopEngine(); // Calls default method
}
}
මේ උදාහරණයේදී Drivable
interface එකට startEngine()
සහ stopEngine()
කියලා default methods දෙකක් තියෙනවා. Car
class එක startEngine()
method එක override කරලා තියෙනවා, ඒත් stopEngine()
method එක use කරන්නේ interface එකේ තියෙන default implementation එක. Bicycle
class එකත් startEngine()
method එක override කරලා තියෙනවා, මොකද Bicycle එකකට engine එකක් නැති නිසා.
මේකෙන් වෙන්නේ, Interface එකක් වෙනස් කළාම, කලින් ඒක implement කරලා තිබ්බ classes කැඩෙන්නේ නැති එක. ඒවාට පුළුවන් continue කරන්න, අවශ්ය නම් අලුත් method එක override කරන්නත් පුළුවන්.
Default methods, Java වල functional programming concepts (Lambda Expressions) එක්කත් හොඳට වැඩ කරනවා. උදාහරණයක් විදිහට, java.util.Iterable
interface එකට Java 8 වලදී forEach()
කියන default method එක එකතු කළා. ඒක නිසා අපි Collection එකක් iterate කරන්න කලින්ට වඩා ලේසි ක්රමයක් ලැබුණා, ඒත් කලින් තිබ්බ code base එකට කිසිම බලපෑමක් වුණේ නෑ.
Static Methods: Interface එකටම අයිති Methods (Methods Belonging to the Interface Itself)
Default methods වගේම, Java 8 වලදී Interfaces වලට static methods add කරන්නත් පුළුවන් වුණා. Static methods කියන්නේ class එකකට හෝ interface එකකට අයිති methods මිසක්, object එකකට අයිති methods නෙවෙයි. මේවාට instance variables වලට access කරන්න බෑ. Interface එකක static methods, ඒ Interface එකේ නමින්ම විතරයි call කරන්න පුළුවන්, implement කරන class එකක object එකකින් call කරන්න බෑ.
උදාහරණයක් (Example):
public interface Calculator {
// A default method (can be overridden)
default int add(int a, int b) {
return a + b;
}
// A static method (cannot be overridden, belongs to the interface)
static int multiply(int a, int b) {
return a * b;
}
static double divide(double a, double b) {
if (b == 0) {
throw new IllegalArgumentException("Cannot divide by zero");
}
return a / b;
}
}
public class MyCalculator implements Calculator {
// We can implement abstract methods if any, or override default methods
// But we cannot override static methods here
}
public class StaticMethodDemo {
public static void main(String[] args) {
// Calling static methods of the interface
int product = Calculator.multiply(5, 4);
System.out.println("Product: " + product); // Output: Product: 20
double divisionResult = Calculator.divide(10.0, 2.0);
System.out.println("Division: " + divisionResult); // Output: Division: 5.0
// You CANNOT call static methods on an instance of the implementing class:
// MyCalculator mc = new MyCalculator();
// mc.multiply(2, 3); // This would cause a compile error!
// You can call default methods on an instance
MyCalculator mc = new MyCalculator();
int sum = mc.add(7, 3);
System.out.println("Sum using default method: " + sum); // Output: Sum using default method: 10
}
}
මේ උදාහරණයේදී, Calculator
interface එකට multiply()
සහ divide()
කියලා static methods දෙකක් තියෙනවා. මේ methods implement කරන MyCalculator
class එකක object එකකින් call කරන්න බෑ. ඒවා call කරන්න ඕනේ Calculator.multiply(5, 4)
වගේ Interface එකේ නම පාවිච්චි කරලා. මරු කතාව නේද?
කවදාද Default/Static Methods පාවිච්චි කරන්නේ? (When to Use Default/Static Methods?)
Default Methods:
- Backward Compatibility: දැනටමත් use කරන Interface එකකට අලුත් method එකක් add කරන්න ඕනේ නම්, ඒක implement කරලා තියෙන classes වලට බලපෑමක් නොකර මේක කරන්න පුළුවන්.
- Common Utility Methods: Interface එකට අදාළව, ඒක implement කරන classes වලට පොදුවේ අවශ්ය වෙන utility methods provide කරන්න පුළුවන්. මේවා classes වලට අවශ්ය නම් override කරන්නත් පුළුවන්.
- Functional Interfaces: Lambda expressions වලට support කරන්න. උදාහරණයක් විදිහට Stream API එකේ methods (
filter
,map
,reduce
) default methods විදිහට තමයි Interfaces වලට add කරලා තියෙන්නේ.
Static Methods:
- Utility Methods for the Interface: Interface එකේ concept එකට අදාළ, නමුත් object instance එකක් එක්ක සම්බන්ධ නැති utility methods තියාගන්න. උදාහරණයක් විදිහට, factory methods (ඒ කියන්නේ, interface එකක් implement කරන object එකක් return කරන methods)
- Helper Functions: Interface එක ඇතුළේ වෙන methods වලට උදව් කරන helper functions තියාගන්න.
- No Instance Dependency: මේ methods වලට instance specific data අවශ්ය වෙන්නේ නෑ.
අවසන් වශයෙන් (Finally)
Java 8 වලදී Interface වලට default methods සහ static methods එකතු කිරීම Java language එකට විශාල වටිනාකමක් එකතු කළා. මේවා නිසා Interface වල flexibility එක වැඩි වුණා. ඒ වගේම, කලින් තිබ්බ code base එකට හානියක් නොකර අලුත් features එකතු කරන්න developers ලට පුළුවන් වුණා. Functional programming concepts Java වලට integrate කරන්නත් මේවා ගොඩක් උදව් වුණා.
ඉතින් මොකද කියන්නේ? දැන් ඔබට Java 8 වල default methods සහ static methods ගැන හොඳ අවබෝධයක් තියෙනවා ඇති කියලා හිතනවා. මේ concepts නිතරම use වෙනවා modern Java development වල. ඔබත් මේ concepts ගැන අත්හදා බලලා ඔබේ අදහස් පහළ comment section එකේ කියන්න. මොකද, theory විතරක් මදි, practice කරන්නත් ඕනේ. අපි ඊළඟ ලිපියකින් හමුවෙමු! ජය වේවා!