This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Few simple questions" Watch "Few simple questions" New topic
Author

Few simple questions

Sumanth Shanbhag
Greenhorn

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

Regards,
AcE
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
Sheriff

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
sscce.org
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!!!

Sid
Alan Smithee
Greenhorn

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
Greenhorn

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.

D K
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Few simple questions
 
Similar Threads
NX:database design
OpenGL
A free mock test for SCJP 5
Call by ref . & call by value
Passed SCEA5 Part 1 with 67%