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?

1 Answer 1

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.

6
  • 2
    right- so it's like java where you can't do multiple inheritance per se, but you can do it with interfaces? May 21, 2015 at 15:45
  • Yeah, Thats correct May 21, 2015 at 15:45
  • how many levels salesforce support inheritance?
    – Prakash
    Dec 7, 2016 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 Jan 26, 2017 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
    – LVS
    Sep 17, 2019 at 16:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .