Want a Free Job Posting?

Buy a job posting today and the second one is on us. For a limited time only. Act Now.

Interview Question

Interview Chennai (India)

Given a situation where in you can choose either

  composition or inheritance, which one would you choose? and why?
java, high level design

Interview Answer

2 Answers



karthik on Aug 8, 2012

It is always advised to use composition over inheritance. • Inheritance doesn't allow us to have control over the super class being visible to the client. But composition gives us the ability to have control over the visibility. • Unit testing becomes easy in composition because we know what all methods we use in our class rather than writing tests for all the methods in a super class which we inherit. • Generally when there exists an "IS A" relation we use Inheritance, and when a "HAS-A" relationship exists then we use Composition.

Varun R on Oct 28, 2014

Add Answers or Comments

To comment on this, Sign In or Sign Up.