-1

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
afuzzyllama
  • 6,538
  • 5
  • 47
  • 64

3 Answers3

1

You might want to look at this post on StackOverflow - 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");
    }
}
Community
  • 1
  • 1
Tom
  • 2,369
  • 13
  • 21
0

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

Amir Kost
  • 2,148
  • 1
  • 16
  • 30
0

C++ needs destructors to release resources held by an object. The most frequent need is to dispose of dynamically allocated memory.

In Java, dynamically allocated memory is disposed automatically by garbage collection. For this, a destructor is not necessary.

Other resources can be disposed of in via an explicit method. You can call this method in a finally block, or in Java 7, with a try-with-resources. Regardless, consider implementing an interface like java.io.Closeable that indicates explicit disposal is required, and provides a method to do so.

For this program, a close translation of the main method would be:

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

    try {
       atimer.close();
       abomb.close();
    } catch ( java.io.IOException e ) {
        System.err.println( "An error occurred." );
    }
}
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • Thank you very much, the code seems legit. I still have one question though...In C++ the code gives the output... Install a timer Install a timer Install a timer in bomb Demolition of timer Bomb exploded Demolition of timer – user2292121 Apr 17 '13 at 19:28
  • You're getting output of Timer's constructor and destructor twice because you've declared Bomb is-a Timer. Consequently, constructing/destructing a Bomb requires constructing/destructing a Timer. By the way, welcome to Stack Overflow. If you consider this the best answer, click the checkmark to its left. You can upvote multiple answers you find useful by clicking the up-arrow to their left. – Andy Thomas Apr 17 '13 at 19:43
  • Thank you Very much, yes I consider your answer the best one and everyone did give me some insight on the matter. Unfortunately it says I don't have enough reputation. Don't worry I definitely won't forger my first question here as the world of programming is just opening up for me. – user2292121 Apr 17 '13 at 20:14
  • @AndyThomas-Cramer - I up-voted to protect your rep ;-) bugs me when people downvote w/o an explaination – Tom Apr 17 '13 at 23:42
  • @Tom - Thanks. I'm fine with being down-voted for good cause, but would like to know the cause. This one seemed rather random. – Andy Thomas Apr 18 '13 at 03:22