Aryan PrajapatKnowledge Contributor
Although inheritance is a popular OOPs concept, it is less advantageous than composition. Explain.
Although inheritance is a popular OOPs concept, it is less advantageous than composition. Explain.
A class’s testability is improved through composition over inheritance. If a class is comprised of another class, it is simple to create a mock object to simulate the combined class for testing purposes. This privilege is not given by inheritance. Even while Composition and Inheritance both let you reuse code, Inheritance has the drawback of breaking encapsulation. If the function of the subclass depends on the superclass’s action, it suddenly becomes vulnerable. Sub-class functionality may be broken without any alteration on the part of the super-class when its behaviour changes.