• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Few simple questions

 
Sumanth Shanbhag
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

If you're looking for an introductory text, you might try The Java Tutorial.
 
Sidd Kulk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • 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
 
Alan Smithee
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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.
 
Durga Krishna
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic