0

I was preparing for PD1 exam and came across the below sentence on interface in the developer documentation which I’m unable to understand. Can anyone help with an example as to what the below statement implies?

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.

7
  • This "in any place you can use another data type name" is wrong. The following sentence is a bit more helpful. See developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… and Google as it is a common feature in other languages e.g. Java.
    – Keith C
    Dec 23 '20 at 12:49
  • Thanks @KeithC. Can you provide an example please? Dec 23 '20 at 13:02
  • @KeithC Yes, you can use an interface in any place you can use another data type name. That's a perfectly accurate description, unless you have a counterexample?
    – sfdcfox
    Dec 23 '20 at 13:58
  • I've marked this question as a duplicate, citing three questions we have here on SFSE that provide examples of interfaces and how to use them. If you're still confused after reading through those, feel free to let us know, we can reopen your question for you once you edit in any additional questions you may have.
    – sfdcfox
    Dec 23 '20 at 14:00
  • Hi @sfdcfox, can you please give an example to understand the below statement. I am super confused. 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. Dec 23 '20 at 14:07
2

In addition to this question, this question, and this question, here's some additional information.

Interfaces are used to guarantee that a given method exist on an object. This allows generic algorithms to use data types that may not exist yet. This is explored extensively in the questions above.

As a final example, consider the following code:

interface i1 {
  void method1();
}
interface i2 extends i1 {
  void method2();
}
class c {
  void method1() { }
  void method2() { }
}
class c1 implements i1 {
  void method1() { }
  void method2() { }
}
class c2 implements i2 {
  void method1();
  void method2();
}

Given all this, the following is allowed:

i1 value1 = new c1();
i1 value2 = new c2(); // Allowed, i2 implements i1
i2 value3 = new c2();

While the following is not:

i1 value1 = new c(); // does not implement i1
i2 value2 = new c1(); // does not implement i2

As you can see, the variable's data type determines which objects it can hold. The difference here is that c1 and c2 are unrelated to each other. They share some common methods as defined by the interface, but they can do completely separate things.

For other examples, please read the answers linked at the top of this answer. They go in to a lot of detail and examples. This answer is meant to supplement those answers with details about interface inheritance.

1
  • Thank you @sfdcfox for the clear answer. Now I’m able to understand it. Dec 23 '20 at 14:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.