Core Java - Interview Questions and Answers for 'Composition' | Search Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Core Java - Interview Questions and Answers for 'Composition' - 7 question(s) found - Order By Rating

 Q1. Does use of Final class enforces composition over Inheritance in Java ? Core Java
Ans. Yes, to a certain extent. But the objective for Final class could be beyond just enforcing composition as certain classes might have been created without inheritance or composition in mind.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     final class   composition  object oriented programming (oops)  oops concepts  inheritance  object oriented programming (oops)  oops concepts  composition  object oriented programming (oops)  oops concepts vs inheritance  object oriented programming (oops)  oops concepts      expert


 Q2. What is composition ?Core Java
 This question was recently asked at 'MST Solutions'.This question is still unanswered. Can you please provide an answer.


  Sample Code for composition

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     composition  object oriented programming (oops)  oops concepts  oops concepts     Asked in 1 Companies      basic        frequent


 Q3. How are the concept of Association related to Composition and Inheritance ?Core Java
Ans. Composition and Inheritance are the different types of Associations for Classes.

Composition is a has-a association between classes.
Inheritance is a is-a association between classes.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     composition  object oriented programming (oops)  oops concepts  inheritance  object oriented programming (oops)  oops concepts  association  relationship between objects


Frequently asked to fresh graduates and less experienced.
 Q4. Which of the following is tightly bound ? Inheritance or Composition ?Core Java
Ans. Inheritance.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     inheritance  object oriented programming (oops)  oops concepts   java   oops   composition  object oriented programming (oops)  oops concepts     Asked in 1 Companies      basic        frequent


Frequently asked to fresh graduates and less experienced.
  Q5. Difference between Composition and Inheritance ?Core Java
Ans. Inheritance means a object inheriting reusable properties of the base class. Compositions means that an abject holds other objects.

In Inheritance there is only one object in memory ( derived object ) whereas in Composition , parent object holds references of all composed objects.

From Design perspective - Inheritance is "is a" relationship among objects whereas Composition is "has a" relationship among objects.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   oops   oops concepts   inheritance  object oriented programming (oops)  oops concepts   composition  object oriented programming (oops)  oops concepts   difference between   basic interview question     Asked in 12 Companies      basic        frequent

Try 2 Question(s) Test


 Q6. Can we compose the Parent Class object like this ?

class BuggyBread1 extends BuggyBread2 {

private BuggyBread2 buggybread2;

public static void main(String[] args){
buggybread2 = new BuggyBread2();
}
}
Ans. Yes.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   composition   inheritance   yesno

Try 1 Question(s) Test


Frequently asked to fresh graduates and less experienced developers. Among the first few questions in many interviews.
  Q7. What are the difference between composition and inheritance in Java?Core Java
Ans. Composition - has-a relationship between objects.
Inheritance - is-a relationship between classes.

Composition - Composing object holds a reference to composed objects and hence relationship is loosely bound.
Inheritance - Derived object carries the base class definition in itself and hence its tightly bound.

Composition - Used in Dependency Injection
Inheritance - Used in Runtime Polymorphism

Composition - Single class objects can be composed within multiple classes.
Inheritance - Single class can only inherit 1 Class.

Composition - Its the relationship between objects.
Inheritance - Its the relationship between classes.

  Sample Code for inheritance

  Sample Code for composition

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   java concepts   inheritance  object oriented programming (oops)  oops concepts   composition  object oriented programming (oops)  oops concepts   inheritance  object oriented programming (oops)  inheritance vs composition  object oriented programming (oops)  oops concepts     Asked in 29 Companies      basic        frequent

Try 5 Question(s) Test


 Q8. What is the relationship between Vehicle and Engine in this example ?

public class Vehicle {
   Enginer engine;
   public void move(){
      engine = new Engine();
      engine.start();
   }
}
Core Java
a. Composition ( Vehicle has a Engine )
b. Composition ( Engine has a Vehicle )
c. Inheritance ( Vehicle is a Engine )
d. Inheritance ( Engine is a Vehicle )

Ans.a. Composition ( Vehicle has a Engine )

 Q9. What is the relationship between Car and Vehicle in the following code ?

public class Car extends Vehicle{
   Engine engine;   
   
   public static void main(String[] args){
      Vehicle vehicle = new Car();
      car.move();
   }   

   public void move(){
      engine = new Engine();
      engine.start();
   }
}
Core Java
a. Composition ( Vehicle has a Car )
b. Composition ( Car has a Vehicle )
c. Inheritance ( Vehicle is a Car )
d. Inheritance ( Car is a Vehicle )

Ans.d. Inheritance ( Car is a Vehicle )

 Q10. What is the problem with the following code ?

public class Car extends Vehicle{
   Vehicle vehicle;
   
   Car(){
      super();
      this.vehicle = new Vehicle();
   }
}
Core Java
a. There is an Inheritance as well as Composition relationship between Vehicle and Car which is not permitted
b. We cannot initialize the parent class instance within the constructor
c. Call to super is illegal
d. There is no problem

Ans.d. There is no problem


Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: