1

Can you declare variables in Interface in Apex (Salesforce).

In Java, it allows you to declare and initialize variables with public static final keywords in Interface.

Also in Apex, are all abstract methods are public by default?


Thanks guy for the response. Actually my second question was for interface itself in Salesforce. All methods in interface are abstract without implementation right? What accessibility do they have by default. Are they public?

4

Can you declare variables in Interface in Apex (Salesforce).

No.

Also in Apex, are all abstract methods are public by default?

No. All methods and classes are private by default. You must provide an access modifier to grant more than private access. If the methods exist inside an inner class, the abstract methods can be implemented, but if they are not at least protected, you will not be able to implement them in other classes:

public abstract class C1 {
  abstract void method1();
  public abstract void method2();
}

public class C2 implements C1 { // compiler error: must implement method1
  override void method1() { // Compiler error: @Override specified for non-overriding method
  }
  public override void method2() {
  }
}

The following is okay:

public class C1 {
    abstract class C2 {
        abstract void z();
    }
    class C3 extends C2 {
        override void z() {

        }
    }
}

Interface methods do not have a default access modifier, as it is simply a description of what methods (and their return values/parameters) are. Implementing classes must make the methods public or global.

| improve this answer | |
1

Can you declare variables in Interface in Apex (Salesforce).

No, if you want to declare variables, you must use an abstract or virtual class.

Also in Apex, are all abstract methods are public by default?

No, you can have abstract methods with any access modifier.

| improve this answer | |
  • Thanks guy for the response. Actually my second question was for interface itself in Salesforce. All methods in interface are abstract without implementation right? What accessibility do they have by default. Are they public? – Faiz Aug 28 '19 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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