Search code examples
oopinheritancepolymorphismencapsulationabstraction

Is inheritance necessary for encapsulation, abstraction and polymorphism?


Today I had an interview for software engineering position. I have read many things from stackoverflow for the interview. They asked me about the normal things realated to OOP. But they also asked me about these:

Is Encapsulation possible without inheritance?

Is Abstraction possible without inheritance?

Is Polymorphism possible without inheritance?

I have answered these according to my knowledge of OOP. I don't know whether it is right or wrong. Can anyone tell me the correct answer of these with some related examples.

Thanks


Solution

    • Is Encapsulation possible without inheritance?

    Yes, because Encapsulation is the ability to hide a class properties from the outside world by means of access methods.

    • Is Abstraction possible without inheritance?

    Well, abstraction can refer to many things, but talking about OOP: No, an abstract class cannot be used directly, you can only instantiate inherited classes.

    • Is Polymorphism possible without inheritance?

    Yes, polymorphism is the construction of a single interface to several types of objects, for instance, a single function call that can receive different classes or data types as arguments. They can be inherited or not.