12

I should know this, but does Apex support multiple inheritance? I used to do it in C++. I 'm trying to model a class that looks like:

Mother -->kid <--- Father

Also what about hybrid? where the mother and father are derived from a single parent?

18

Syntax for Apex is mostly based on Java and it does not allow multiple inheritance.

Hybrid (Not allowed)

Virtual Class A
Virtual Class B extends A
Virtual Class C extends A
Class D extends B, C

Multiple (Not allowed)

Virtual Class A
Virtual CLass B
Class C extends A, B

Multi level (Yes)

Virtual class A
Virtual class B extends A
Class C extends B

Salesforce does allow multiple interface implementation. To inherit any class, it must be defined with keyword Virtual.

You can read more about inheritance here.

| improve this answer | |
  • 2
    right- so it's like java where you can't do multiple inheritance per se, but you can do it with interfaces? – PartOfTheOhana May 21 '15 at 15:45
  • Yeah, Thats correct – Jitendra Zaa May 21 '15 at 15:45
  • how many levels salesforce support inheritance? – Prakash Dec 7 '16 at 1:26
  • 2
    Prakash, your question made me curious and I wrote a script to generate classes extending each other in case the limit was high or weird behavior would come out at larger levels, but the limit is only 10. If you go beyond that, you get: Maximum type depth exceeded: 10 – James Sullivan Jan 26 '17 at 2:56
  • I'm having trouble do the multi level. Using the example you provided, I have a virtual method in class A that I need to extend in both B as well as C. But I am forced to use the virtual keyword for the method definition in B, so I cannot override it in C. any thoughts? Edit: a minute after I said that, I tried using both virtual and override on the method definition and it seems to accept it. going to see if it actually works – Vid L Sep 17 '19 at 16:23

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.