Trying to understand a question I got wrong on a test:
How does inheritance differ from implementing interfaces?
- With inheritance, a class gains behavior from its superclass.
- With interfaces, a class gains behavior from the interface it implements. (this is the one I chose)
- With inheritance, a class must implement the methods defined by its superclass.
- With interfaces, a class gains both instance variables and behaviors from the interface it implements.
The way I was thinking is that interfaces define behavior, while superclasses define characteristics... or are they the same? Or am I completely backwards in my understanding?
Edit: I guess I should specify that I do know the difference between interfaces and inheritance. I'm just wondering about the two options which use the term behavior. I don't know if the prof was nitpicking about terminology, or if he asked the question poorly.
I know that when you implement an interface, you have to implement all the methods as defined in the interface. As such, I would say that the interface defines the behavior that a class must have, but extending another superclass (although it does also define some behaviors (more can be given to the subclass), it doesn't seem to fit as strongly as the interface defining behaviors. If the class implements an interface, you can be sure that it has certain behaviors.
Maybe the question was meant to ask whether or not the interface itself has the code for the behavior, or if it's just the definition - which if worded that way, I would have known the answer.
gains.super- assuming the super constructor has no "behavior") 2) No. interfaces define just names of methods you need to have. 3) No, unless it's an abstract class. 4) No, Interfaces define method names only.