r/learnjava • • Jun 01 '21

Help translating from c++ to java

Hey, could you please help me translation this code to java?

class DialogDirector {
public:
virtual ~DialogDirector();
virtual void ShowDialog();
virtual void WidgetChanged(Widget*) = 0;
protected:
DialogDirector();
virtual void CreateWidgets() = 0;
};

additionally what does -> and :: mean in c++, and how could i translate it to java?

0 Upvotes

8 comments sorted by

View all comments

1

u/[deleted] Jun 01 '21

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();
}