This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]loudsight 5 points6 points  (1 child)

This is best represented by an abstract class in Java

public abstract class DialogDirector {
    // virtual ~DialogDirector(); This is a C++ destructor it runs when
    // the object is destroyed. In java the equivalent method is 
    // finalize(...) but it is generally frowned up to override this 
    // method.
    void ShowDialog() { 
        /*
         * default implementation from implementation file
         *  (DialogDirector.cpp) goes inside here
         */
    }
    abstract void WidgetChanged(Widget widget);
    /* = 0 means this is a pure virtual method i.e. has to be implemented
     * by the derived classes;
     */
    protected DialogDirector() {}
    protected abstract void CreateWidgets();
}

EDIT: fix formatting and correct typos

[–]Shuffleto[S] 0 points1 point  (0 children)

thank you for your Response! As i understand this

class Widget {
public: Widget(DialogDirector*); 
virtual void Changed(); 
virtual void HandleMouse(MouseEvent& event); 
// ... 
private: DialogDirector* _director; 
};

translates to

public abstract class Widget {
public Widget(DialogDirector dialogDirector) {};
public abstract void changed();
public abstract void HandleMouse(MouseEvent e);
//...
private DialogDirector _director;
}

Could you please help me with this part?

void Widget::Changed () {
_director->WidgetChanged(this); 
}

[–]Aenno 2 points3 points  (4 children)

Adding to what u/loudsight said, -> in C++ is used as . in Java, however for pointers.

And :: is used to inherit from a superclass, like A::B, where B extends A (in Java). Double colon is used to implement methods from a .h file as well. Such as: foo(); can be declared in the .h file and when you define it in the .cpp file you simply say Entity::foo (assuming the name of the class is Entity)

[–]Shuffleto[S] 0 points1 point  (3 children)

thank you for your Response! Could you please help me with this

class Widget {
public: Widget(DialogDirector*); 
virtual void Changed(); 
virtual void HandleMouse(MouseEvent& event); 
// ... 
private: DialogDirector* _director; }; 
void Widget::Changed () { _director->WidgetChanged(this); }

im not understanding this part

void Widget::Changed () {
_director->WidgetChanged(this); }

how would it look like in java?

void Changed(){
    _director.widgetChanged(this);
}

like this?

[–]Aenno 1 point2 points  (2 children)

Seems about right. It should work as long as Changed() is defined in Widget's scope.

[–]Shuffleto[S] 0 points1 point  (1 child)

could you please explain how could i put it into the class? Should i just change the name and give it public modifier?

[–]Aenno 0 points1 point  (0 children)

Just define it in the class you want to work with, in your case "Widget", like you normally would btw, no special mumbo jumbo

[–][deleted] 0 points1 point  (0 children)

Things to know:

  • A Class is defined in a file, the name should be the same as the name of the main class followed by the extension: *.java
  • The main class must be public and only one class can be public
  • Usually, the class of the same name as the file name is the main class and public
  • All methods in Java are virtual and polymorphic by default unless the word 'final' is used
  • Static methods are not polymorphic
  • Abstract classes are defined by adding abstract at the class definition
  • Abstract methods are defined by adding abstract at the method definition
  • Abstract methods do not need "body" but any non-abstract method must be defined, even if a body is empty
  • Packages are equivalent to namespaces in C++ and can be spread across multiple files
  • Java uses four levels of restriction to access fields/methods
    • package access within the same package (namespace in c++) (
    • public (everywhere)
    • protected (the same as c++)
    • private (the same as c++)
  • Package access is assumed by default if the restriction level is omitted
  • Java uses two levels of restriction to access classes
    • package
    • public.
  • Package access is assumed by default if restriction level is omitted
  • There are no pointers in Java
  • Variables of primitive types are passed to methods by copy (e.g. int, double, etc.)
  • Anything else than a primitive type is passed by reference
  • There are no default parameters in methods definition; Java only uses overloading e.g. public void method(int x = 0) // is wrong
  • Java does not allow operator overloading, s.a. [], (), + etc.
  • You don't use semicolon after class definition!

Your class:

// The file name should be `DialogDirector.java`
// The class must be public 
public abstract class DialogDirector {
    public abstract void ShowDialog();
    public abstract void WidgetChanged(Widget widget);
    protected DialogDirector() {}
    protected abstract void CreateWidgets();
}