function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Andrea GelsominoAndrea Gelsomino 

Interface class example

Hi, 
I really can't understand this error on this easy Interface exampe, can you please help me to understand how can i use  Interface classes on apex code ?
please have a look to the attach.

thanks in advanceinterface

Best Answer chosen by Andrea Gelsomino
Nirmala  KuchiNirmala Kuchi
Hi Andrea,

Only one independent class/interface is allowed to define in each class. This issue can be solved in two ways:

1. Create different classes for each class and interface.
2. Create and Outer class and keep both the class and interface inside it, as below:

public class OuterClass {

    public interface PurchaseOrder {
        Double discount();
    }

    public class test1 implements PurchaseOrder {
        
        public Double discount() {
            return .05;
        }
    }
}

Thanks,
Nirmala

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please check below post.  I hope that will help you
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_interfaces.htm

Dnt save Both class in  one class.

Create one class like below
// An interface that defines what a purchase order looks like in general
public interface PurchaseOrder {
    // All other functionality excluded
    Double discount();
}
Then 2nd class like below
// One implementation of the interface for customers
public class CustomerPurchaseOrder implements PurchaseOrder {
    public Double discount() {
        return .05;  // Flat 5% discount
    }
}
Let us know if this will help you

Thanks
Amit Chaudhary
DeepthiDeepthi (Salesforce Developers) 
Hi Andrea,

Try to save the Interface(i.e., PurchaseOrder) in one class and CustomerPurchaseOrder in another class. You can execute them without errors.

Best Regards,
Deepthi
Nirmala  KuchiNirmala Kuchi
Hi Andrea,

Only one independent class/interface is allowed to define in each class. This issue can be solved in two ways:

1. Create different classes for each class and interface.
2. Create and Outer class and keep both the class and interface inside it, as below:

public class OuterClass {

    public interface PurchaseOrder {
        Double discount();
    }

    public class test1 implements PurchaseOrder {
        
        public Double discount() {
            return .05;
        }
    }
}

Thanks,
Nirmala
This was selected as the best answer
Andrea GelsominoAndrea Gelsomino

Thank you so much guys,

I really like your answers, I prefer this one because i could manage everything inside a "package" class , but i don't undestand how can I reference it, actlually this returns me as an error

OuterClass.test  item =  new OuterClass.test1()


public class OuterClass {

    public interface PurchaseOrder {
        Double discount();
    }

    public class test implements PurchaseOrder {
        
        public Double discount() {
            return .05;
        }
    }
}