views:

101

answers:

2

Hi,

For school I need to learn Java and since I'm used to C++ (like Cocoa/Objective-C) based languages, I get really frustrated on Java.

I've made a super-class (that can also be used as a base-class):

public class CellView {
    public CellViewHelper helper; // CellViewHelper is just an example

    public CellView() {
        this.helper = new CellViewHelper();
        this.helper.someVariable = <anything>;

        System.out.println("CellView_constructor");
    }

    public void draw() {
        System.out.println("CellView_draw");
    }

    public void needsRedraw() {
        this.draw();
    }

}

public class ImageCellView extends CellView {

    public Image someImage;

    public ImageCellView() {
        super();
        this.someImage = new Image();
        System.out.println("ImageCellView_constructor");
    }

    public void setSomeParam() {
        this.needsRedraw(); // cannot be replaced by this.draw(); since it's some more complicated.
    }

    @Override public void draw() {
        super.draw();
        System.out.println("ImageCellView_draw");
    }

}

Now, when I call it like this:

ImageCellView imageCellView = new ImageCellView();
imageCellView.setSomeParam();

I get this:

CellView_constructor

ImageCellView_constructor

CellView_draw

However, I want it to be:

CellView_constructor

ImageCellView_constructor

CellView_draw

ImageCellView_draw

How can I do this?

Thanks in advance,

Tim

EDIT:

I also implemented this method to CellView:

public void needsRedraw() {
    this.draw();
}

And this to ImageCellView:

public void setSomeParam() {
    this.needsRedraw(); // cannot be replaced by this.draw(); since it's some more complicated.
}

And I've been calling this:

ImageCellView imageCellView = new ImageCellView();
imageCellView.setSomeParam();

Does this causes the problem (when I call a function from the super it calls to the super only)? How can I solve this... (without having to redefine/override the needsRedraw()-method in every subclass?)

+1  A: 

The short answer is "you can't."

Objects are constructed from the bottom up, calling base class initializers before subclass initializers and base class consrtuctors before subclass constructors.

EDIT:

The code you have looks good, based on your edit. I would go through the mundane tasks like ensuring that you have compiled your code after you've added you System.out.println calls to your subclass

akf
Oh, I'm sorry, I placed the output in the wrong order... I actually mean that ImageCellView_draw isn't called and I don't understand why...
Tim van Elsloo
I updated my code to show you some more methods (including a super-call which may cause the problem?).
Tim van Elsloo
+1  A: 

You should get proper output.

I tried you example just commented unrelated things:

import java.awt.Image;

public class CellView {
    //public CellViewHelper helper; // CellViewHelper is just an example

    public CellView() {
        //this.helper = new CellViewHelper();
        //this.helper.someVariable = <anything>;

        System.out.println("CellView_constructor");
    }

    public void draw() {
        System.out.println("CellView_draw");
    }

    public static void main(String[] args) {
        ImageCellView imageCellView = new ImageCellView();
        imageCellView.draw();
    }
}

class ImageCellView extends CellView {

    public Image someImage;

    public ImageCellView() {
        super();
        //this.someImage = new Image();
        System.out.println("ImageCellView_constructor");
    }

    @Override public void draw() {
        super.draw();
        System.out.println("ImageCellView_draw");
    }

}

and I get following output:

CellView_constructor

ImageCellView_constructor

CellView_draw

ImageCellView_draw

This is exactly what you want, and this is what your code print's.

YoK
Oh, I'm sorry, I placed the output in the wrong order... I actually mean that ImageCellView_draw isn't called and I don't understand why...
Tim van Elsloo
It still doesn't get called (there's no "ImageCellView_draw" in the output).I updated my code to show you some more methods.
Tim van Elsloo
That's weird, this works, but the same code (with other class-names and some imports to awt.Graphics) doesn't work... Looks like there's something wrong with my Graphics-implementation. Anyway, thanks!
Tim van Elsloo
Why I didn't get any points for this question though this was rated up and selected ?
YoK
@Yok You randomly decided to delete massive parts of your question and restore them, which pushed you over the <3 = Community Wiki limit
TheLQ
Thanks @Lord.Quackstar. Question was posted I answered it accordingly. Then asker changed question considerably which made my earlier answer invalid, So I had to delete my earlier answer and post new one. Next time onwards I will mark answer for delete and post new answer :)
YoK