jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Few simple questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Few simple questions" Watch "Few simple questions" New topic

Few simple questions

Sumanth Shanbhag

Joined: May 10, 2007
Posts: 13
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..

Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
How do you force garbage collection in Java ?

No, you cannot, by Java Language Specification!!!
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
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!
marc weber

Joined: Aug 31, 2004
Posts: 11343

Welcome to JavaRanch!

If you're looking for an introductory text, you might try The Java Tutorial.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Sidd Kulk
Ranch Hand

Joined: Feb 20, 2007
Posts: 152
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!!!

Alan Smithee

Joined: Mar 21, 2006
Posts: 23

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.
Durga Krishna

Joined: Apr 05, 2007
Posts: 28
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.

I agree. Here's the link: http://aspose.com/file-tools
subject: Few simple questions
It's not a secret anymore!