Question: I need to convert this program to java. I can handle everything else but the destructor. Now I've already read all over about the GC(garbage collector) and it unreliability. I am thinking why put a destructor for me to convert anyways if there wasn't a way or something.

```
#include<iostream>

using namespace std;

class Timer{
      public:
             Timer();
             ~Timer();
      };
Timer::Timer(){
               cout<<"Install a timer"<<endl;
               }
Timer::~Timer(){
                cout<<"Demolition of timer"<<endl;
                }

class Bomb: public Timer{
      public:
             Bomb();
             ~Bomb();
             };
Bomb::Bomb(){
             cout<<"Install a timer in bomb"<<endl;
             }
Bomb::~Bomb(){
              cout<<"Bomb explode..."<<endl;
              }

int main()
{
    Timer* aTimer = new Timer;
    Bomb* aBomb = new Bomb;
    delete aTimer;
    delete aBomb;

    system("pause");
    return 0;
}

```

So far, what I came up with was this stuff using Eclipse...

```
public class mainting{

    public static void main(String test[])
    {
        timer atimer = new timer();
        bomb abomb = new bomb();

    }
}

public class bomb extends timer {
    public bomb(){
        System.out.println("Install a timer in bomb");

    }

}

public class timer {

    public timer()
    {System.out.println("Install a timer");}

}

```

pretty straight forward I know.

This is the output of the code in C++

```
Install a timer
Install a timer
Install a timer in bomb
Demolition of timer
Bomb exploded
Demolition of timer

```

Answer: 
You might want to look at this post on [StackOverflow](https://stackoverflow.com/questions/2506488/java-finalize-method-call) - read it carefully though, as `finalize()` is not the same as C++ destructors!! You cannot make assumptions about whether it's called, what objects are still available, etc.

I'm not sure if Java's `finalize` is considered harmful.

For the short program you have on your exam, it's unlikely that (if you provided a `finalize()` method in the Java version), the GC would ever call it. But, you can demonstrate that you know how it works, and what the differences are between C++ and Java w.r.t object destruction.

EDIT:

In the C++ program, the last three lines of output are associated with the destruction of a C++ Timer object, and then a C++ Bomb object, but the line `Demolition of timer` appears twice - I think your prof might be trying to demonstrate that in C++, because Bomb inherits from Timer, the destructors are called in the order of most-derived class to the base class.

On a side note, the C++ destructors should be made `virtual`, but you'll probably learn that sometime in the future.

As an alternative to using the `finalize()` feature, you could try this:

```
public class mainting{

    public static void main(String test[])
    {
        timer atimer = new timer();
        bomb abomb = new bomb();

        atimer.destroy();  // since no destructor in Java, add a "destroy()" method
        abomb.destroy();

    }
}

public class bomb extends timer {
    public bomb(){
        System.out.println("Install a timer in bomb");

    }

    public void destroy(){
        System.out.println("Bomb exploded");
        super.destroy(); // destroy parent object
    }
}

public class timer {

    public timer()
    {System.out.println("Install a timer");}

    public void destroy() {
      System.out.println("Destruction of timer");
    }
}

```
You can either use try-with-resources or use the `PreDestroy` annotation.