eBay Inc.

  www.ebay.com
  www.ebay.com

Interview Question

Software Developer Interview Chennai (India)

Given a situation where in you can choose either

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

Interview Answer

2 Answers

0

inheritance

karthik on Aug 8, 2012
0

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 Question, Sign In with Facebook or Sign Up