Thank you Satish and Deepak, glad that you find information given here useful. May 9, at PM Arun Singh said June 27, at AM Anonymous said July 25, at AM Unknown said Thanks August 29, at AM Shaheed said Nicely explained October 24, at AM Unknown said April 7, at PM Unknown said December 30, at AM Unknown said Very clearly explained, well done and thank you,,,!
April 27, at AM Anonymous said September 1, at AM. Newer Post Older Post Home. Subscribe to: Post Comments Atom. Subscribe for Discounts and Updates Follow. Search This Blog. Interview Questions core java interview question data structure and algorithm 78 Coding Interview Question 75 interview questions 70 design patterns 35 SQL Interview Questions 34 object oriented programming 34 thread interview questions 30 spring interview questions 28 collections interview questions 25 database interview questions 16 servlet interview questions 15 Programming interview question 6 hibernate interview questions 6.
How to design a vending machine in Java? How HashMap works in Java? Why String is Immutable in Java? Translate This Blog. How to use Lock and ReentrantLock for Synchronizat Top 5 Node. How to solve FizzBuzz Problem in Java 8? Top 25 Vue. How to Clone a Collection in Java? Deep copy of Ar How to use Stream with List and Collection in Java How to fix java.
ClassNotFoundException: com. How to print 1 to without using loop in Java? How to use SynchronousQueue in Java? Prouder Consu Top 10 Microservices Design Patterns and Principle What is Double Brace Initialization in Java?
Overriding equals and hashCode method in Java What is String deduplication in Java? How to Save Difference between map and flatMap in Java 8 S How to use props in React? Top 30 React. What is volatile modifier or volatile field in Jav How to set Java Path and Classpath in Windows 7, Top 6 Online Courses to learn Gatsby. How to remove all special characters from String i Difference between Comparison QuickSort and Non Why Static Code Analysis is Important?
Java 8 Comparator comparing and thenComparing Top 5 Courses to learn WordPress for Beginners in What are React and Redux Hooks? Example Tutorial How to compare objects on natural order in Java? Top 10 Pluralsight Courses for React Developers Difference between Association, Composition and Ag Suppose, there's a game which has different types of soldiers. Each soldier can have a knapsack which can hold different things. Inheritance here? These are types of soldiers. Aggregation here? The knapsack can contain grenades, guns different types , knife, medikit, etc.
A soldier can be equipped with any of these at any given point in time, plus he can also have a bulletproof vest which acts as armor when attacked and his injury decreases to a certain percentage. The soldier class contains an object of bulletproof vest class and the knapsack class which contains references to these items.
Although of course it wouldn't make sense to have a class Shape 'having-a' Point and a Square classes. Here inheritance is due. People tend to think about inheritance first when trying to design something extensible, that is what's wrong.
Favour happens when both candidate qualifies. A and B are options and you favour A. The benefit is not immediately visible. To see the benefit you need to wait for the next unexpected change request. So in most cases those sticked to generlalization fails when compared to those who embraced composition except one obvious case mentioned later.
Hence the rule. From a learning point of view if you can implement a dependency injection successfully then you should know which one to favour and when.
The rule helps you in making a decision as well; if you are not sure then select composition. Summary: Composition :The coupling is reduced by just having some smaller things you plug into something bigger, and the bigger object just calls the smaller object back.
Generlization: From an API point of view defining that a method can be overridden is a stronger commitment than defining that a method can be called. And never forget that with composition you are using inheritance too, from a interface instead of a big class. Both approaches are used to solve different problems. You don't always need to aggregate over two or more classes when inheriting from one class. Sometimes you do have to aggregate a single class because that class is sealed or has otherwise non-virtual members you need to intercept so you create a proxy layer that obviously isn't valid in terms of inheritance but so long as the class you are proxying has an interface you can subscribe to this can work out fairly well.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Inheritance vs. Aggregation [closed] Ask Question. Asked 13 years ago. Active 9 years, 2 months ago. Viewed k times. Improve this question. Craig Walker Craig Walker Related: Prefer composition over inheritance?
Add a comment. Active Oldest Votes. It's not a matter of which is the best, but of when to use what. If The new class is more or less as the original class. Use inheritance. The new class is now a subclass of the original class. If the new class must have the original class. Use aggregation. The new class has now the original class as a member. However, there is a big gray area. So we need several other tricks.
If we have used inheritance or we plan to use it but we only use part of the interface, or we are forced to override a lot of functionality to keep the correlation logical. Then we have a big nasty smell that indicates that we had to use aggregation. If we have used aggregation or we plan to use it but we find out we need to copy almost all of the functionality.
Then we have a smell that points in the direction of inheritance. Lets add an example. Play; Purr; end; Ok, this is nice. Improve this answer. Toon Krijthe Toon Krijthe The "reuse almost all functionality of a class" is the one time I do actually prefer inheritance. What I'd really like is a language that has the ability to easily say "delegate to this aggregated object for these specific methods"; that's the best of both worlds.
I'm not sure about other languages, but Delphi has a mechanism, that let members implement part of the interface. Objective C uses Protocols that let you decide whether your function is required or optional. Great answer with a practical example.
I would design the Pet class with a method called makeSound and let each subclass implement their own kind of sound. At the beginning of GOF they state Favor object composition over class inheritance.
Toon Krijthe Harper Shelby Harper Shelby We put these properties into the model , maxSpeed , and yearOfManufacture fields. As for behavior, any car can accelerate and slow down. What benefits does this give us? First of all, it reduces the amount of code. Of course, we can do without the parent class. But since each car must be able to accelerate and slow down, we'll have to create gas and brake methods in the Truck , Sedan , F1Car , and SportsCar classes and in every other car class.
Imagine how much extra code we would have to write. And don't forget about the model , maxSpeed , and yearOfManufacture fields: if we get rid of the parent class, we'll have to create them in each car class! When we have a couple dozen car classes, the amount of duplicate code becomes really serious. Moving common fields and methods also called "states" and "behaviors" to a parent class lets us save a lot of time and space.
If some type has unique properties or methods that other car types don't have, no big deal. You can always create them in a descendant class, separate from everyone else. Unlike their "relatives", they have a unique behavior — they take a pit stop from time to time. This doesn't bother us. We've already described the common behavior in the Car parent class, and the specific behavior of descendant classes can be added to those classes.
The same is true of fields: if a child class has unique properties, we calmly declare these fields inside the child class and stop worrying. For programmers, it's very important to not write extra code. You'll come across this repeatedly in your work. Please remember something else crucial: Java doesn't have multiple inheritance. Each class inherits only one class. We'll talk more about the reasons for this in future lessons.
For now, just remember it. By the way, this makes Java different from some other OOP languages. Everything is more or less clear with inheritance.
0コメント