• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Few simple questions

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a few simple questions. Could someone please answer them?

1) What is the difference between inheritance and composition ? With an example

2) How do you force garbage collection in Java ?

I would be grateful if somebody answers them..

Regards,
AcE
 
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you force garbage collection in Java ?

No, you cannot, by Java Language Specification!!!
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q. When should I use inheritance, when aggregation?

A: They are Two different relationships!

He is a human, human has a heart!

ISA: inheritance
HASA: aggregation

Composition is strong form of aggregation!
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

If you're looking for an introductory text, you might try The Java Tutorial.
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For IS-A and HAS-A relation ship, say, when you quote 'Car IS-A Vehicle', it means that Car is a type of vehicle(in java terms, its a sub class of vehicle, or it extends vehicle), while when you say 'Car HAS-A Engine', it means that the class Car has a reference to an instance of class Engine. There is no direct relation between these classes.
Hence, one can call a method on Car object to start the engine, and in turn, the car object would internally use the reference to Engine object to start the Car. This way the user doesn't have to worry about creating an instance of Engine, because Car HAS-A Engine.

Also, there might be other type of Vehicles which would need this Engine class reference. This way it avoids redundancy.
That's OOP!!!

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


2) How do you force garbage collection in Java ?



As mentioned above, you can't.

That being said...

Every object in Java inherits the finalize() method from java.lang.Object
This is the method that is called by the garbage collector when no reference variables to the object exist. However, it really isn't an order to clean the object up. It's more of a suggestion. By design, there is nothing you can do that will 100% clean unreferenced objects up.

I hope that helps.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you force garbage collection in Java ?
We can force the garbage collection in java by calling the method System.gc().But there is no gaurentee that the object will be garbage collected.Because,if the heap is full with more than 80% then only garbage collector comes into picture.

D K
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic