Question

1. Discuss the difference between inheritance and composition. What are the advantages and disadvantages of each?...

1. Discuss the difference between inheritance and composition. What are the advantages and disadvantages of each? Can you implement one with the other?

2.How is a class similar to a database table? How is it different? How do these similarities and differences justify the need for class models and for data models? Or do they?

3.Discuss the difference between association and composition. What are the advantages and disadvantages of each?

4.When would you apply inheritance? When would you not? Provide examples

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Inheritance and Composition both provides code re usability but are absolutely different the way they provide code re usability. Inheritance is an “is-a” relationship. Composition is a “has-a”. Inheritance provides code re usability by extending a class. In inheritance final class cannot be extended whereas in composition final class can also be reused.

Composition is done by having an instance of another class as a field of class, instead of extending a class as in inheritance.

Advantages of Inheritance:

  1. Helps code re usability.
  2. Improves program structure and makes it more readable.
  3. Codes become easy to debug.
  4. It makes code more flexible to change

Disadvantages of Inheritance:

  1. Two classes become tightly coupled that the classes become dependent on each other.
  2. If any functionality of base class changes then the changes also reflects in child class.
  3. Consumes time and efforts to jump through different levels of the inheritance.

Advantages of Composition:

  1. Facilitate code reusability via a “has-a” relationship.
  2. Flexible than inheritance as implementation of class can be changed at run time by changing included objects.
  3. Based on object composition usually have less classes.
  4. Supports multiple inheritance in language which does not support.

Disadvantages of Composition:

  1. Composition requires more code and time effort.
  2. Design based on object composition usually have more objects.
Add a comment
Know the answer?
Add Answer to:
1. Discuss the difference between inheritance and composition. What are the advantages and disadvantages of each?...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT