• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Aggregation And Inheritence

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I want a clear explanation about the difference between Inheritence and Aggregation along with a small code example.
Can anyone help me

Regards,
Dipti
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,
Aggregation : Containment.. One objcet acts like part of another onject.

Inheritance : U get properties from super class(existent class )
like parent & children
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sonali,

Aggregation : Containment.. One objcet acts like part of another onject



Can u explain this point in brief with example
i didnt get it.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There r 2 ways by which we can access the methods or variables declared in another class

1> simply extend that class
e.g.

class A extends B{
//lot of code which accesses methods of class B as these are the methods from class A//
}

This is called Inheritance

2> In second way,we can declare the object of reqired class in our class,& then using that object access the methods of that class
e.g.
class B{}
Class A{
B b = new B();
//then here using b.methodname we can access the methods from class B
}

This is called Aggregation
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi harish and swati,

u explained abt aggregation and inheritance very well

i have seen a question like is-a,has-a relations belongs to inheritance and aggregation how? can anybody explain

thanx in advance
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kotha,

I'll try explain using code.

IS-A Relationships

It's related to inheritance, ie, whenever you have a class that "extends" another class it will pass the "instanceof" check.

Imagine that a Ferrari is a car so ...




HAS-A Relationships

It's related to object contents, ie, any other instance or class members that refers to another class (from same type or not).

Now, let me make a littler changes in our car ...

 
kotha vijaybabu
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx edisandro

i got it

thanx a lot
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, there is also a offset type of aggregation called composition
which basically means same thing but difference is more on conceptual level





They look auwfully similiar and they are, difference is in concept....
as far as litterature states :
Composition is type of aggregation but they are not the same thing conceptualy,
first example states that agregated Object, in that case Car, can exist independently of the Agregator, cars can be outside of garage
and second example states that agregated Object is a part of the whole unit and should not exist without it .this case engine is part of the car, and you can't just buy only engine to drive to work with ...
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
your explaination has cleared my doubts
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thax swati for explaining in simple way.
i had also same doubt as dipti.th a lot.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic