• Post Reply Bookmark Topic Watch Topic
  • New Topic

when to use aggregation and when to use inheritance  RSS feed

 
abhijeet kush
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any cost related to use of aggregation and inheritance ???Which one is better to use and when should we use either of them??
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"is a" versus "has a" relationship. A car has wheels. A car is a vehicle.

No decision should be made on "cost" -- but rather what makes the most sense for the model.
 
abhijeet kush
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we use inheritance then does it increases the size of subclass(all the methods of superclass are added to subclass )
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abhijeet kush wrote:If we use inheritance then does it increases the size of subclass(all the methods of superclass are added to subclass )
Not at all. Whichever technique you use, the classes have the same number of methods: those you wrote and those inherited from superclasses. Each method is recorded once for each time it is written, so overridden methods are duplicated. So there is no need to worry about using too much memory.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While using HAS A relation we will be using the same method as it was in original class so we ultimately end up creating a duplicate class,but if we are establishing IS A relation we can override the method of class we inherit..

This is how i think while coding and it helps me
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abhijeet kush wrote:If we use inheritance then does it increases the size of subclass(all the methods of superclass are added to subclass )

Who cares? This is the sort of thing that should not be part of the decision of how to model the data.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote: . . .This is the sort of thing that should not be part of the decision of how to model the data.
Even more so when it is probably wrong
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!