Interface in Apex is a collection of unimplemented methods and it contains the method signature, but the body of each method is empty. To use Interface in Apex, another Apex class must implement it by providing the body for all the methods contained in Apex interfaces. Interface in Apex are given as global.
Interfaces includes only method signatures The methods are not implemented in the interfaces Interfaces can provide a layer of abstraction to your code. They separate the specific implementation of a method from the declaration for that method.
For example, a company might have two types of purchase orders, ones that come from customers, and others that come from their employees. Both are a type of purchase order. Suppose you needed a method to provide a discount. The amount of the discount can depend on the type of purchase order
An interface that defines what a purchase order looks like in general
public interface PurchaseOrder { // All other functionality excluded Double discount(); }
One implementation of the interface for customers
public class CustomerPurchaseOrder implements PurchaseOrder { public Double discount() { return .05; // Flat 5% discount } }
Another implementation of the interface for employees
public class EmployeePurchaseOrder implements PurchaseOrder { public Double discount() { return .10; // It’s worth it being an employee! 10% discount } }
When you define a new interface, you are defining a new data type. You can use an interface name in any place you can use another data type name. If you define a variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface, or a sub-interface data type.
Syntax
Public interface Exam { Account Show(Sting S); }
All the methods in the interface are public and abstract methods. Interface also contains, Final Static Data members. The Class which is implemented this interface should define all the methods in the interface.
- Interfaces can provide a layer of abstraction for our code.
- Defining an Interface in Apex is similar to defining a new class in Apex.
Example
interface test { public String Show() void disp(); global Class Example implements test { return 'Prasaanth'; } public void disp( ) { Account a = new Account(Name='Prasanth'); } }
We should define method in the interface, if not it will display an error. For interface in Apex, we can not create an object.
Class Test { } Test t = new Test( ); interface Demo { void Show ( ); }
- Demo d = new Demo ( ); is not allowed because interface in Apex contains only unimplemented methods and static methods.
- Demo d = e; is allowed because ‘e’ is the object of the class which is implementing the demo interface.
- Demo d = t; is not allowed because test class in not implemented interface demo.
Interface Iterator.
This is the interface defined by Salesforce in Apex. Interface Iterator has 2 methods .
Boolean hasNext( )
Boolean hasNext( ) method return true if there is another item in the collection being traverse else returns ‘false’
AnyType next()
AnyType next() method returns the next item in the collection. Both methods given above should be declared as a global or public
How to create Custom Iterator in Apex.
public class Prasanth implements Iterator <Account> { public List <Account> acc; public Integer i; public Prasanth( ) { acc = [SELECT name FROM Account]; i = 0; } public boolean hasNext () { if(i>acc.size()) return false; else return true; } Public Account next() { if(i==o) return null; i++; return a[i-1]; } }
Interface iterable
Iterable is interface in which we have a method Iteratorclass Iterator()
Example1:
class Bened implements Iterable<Account> { public Iterator<Account> iterator() { Prasanth P = new Prasanth(); Iterator<Account> it = s; return it; } }
Example2
public class SampleIterator { public List <Account> acc; public List <Account> getAcc() { return acc; } public SampleIterator() { acc = new List <Account>(); } public pageReference Show() { Iterable <Account> my = new tkartiterable(); Iterator <Account> test = my.Iterator(); while(test.next()!-null) { Account a = test.next(); acc.add(a); } } }
Visualforce page
<apex:page controller="SampleIterator"> <apex:form> <apex:commandButton value="click" action="{!show}"/> <apex:pageBlock> <apex:pageBlockTable value="{!acc}" var="a"> <apex:column value="{!a}"/> </apex:pageBlockTable> </apex:pageBlock> </apex:form> </apex:page>