# From C++ — the same four types, laid out both ways

You already know what this code is *for*. What changes is where it goes.

## The interface

**C++ — `Greeting.h`**

```cpp
#pragma once
#include <string>

class Greeting {
public:
    virtual ~Greeting() = default;
    virtual std::string greet(const std::string& name) const = 0;
};
```

**Java — `Greeting.java`**

```java
public interface Greeting {

    String greet(String name);
}
```

Three things vanished, and none of them are optional in C++:

- **The include guard.** There is nothing to guard. `javac` reads a type declaration once
  no matter how many files mention `Greeting`.
- **The virtual destructor.** There are no destructors. Nothing owns anything.
- **`= 0`, `virtual`, and `public`.** Every method on an interface is already public and
  abstract. Writing `public abstract String greet(String name);` compiles, and every
  reviewer will read it as someone who has not written Java before.

`const` on the method has no Java equivalent either. Immutability is a property of the
*class you write*, never a qualifier on the reference — that is J6 and J4 territory.

## The implementation

**C++ — `Greeter.h` plus `Greeter.cpp`, two files, one class**

```cpp
// Greeter.h
#pragma once
#include "Greeting.h"

class Greeter : public Greeting {
public:
    std::string greet(const std::string& name) const override;
};
```

```cpp
// Greeter.cpp
#include "Greeter.h"

namespace {
    std::string calm() { return "."; }
}

std::string Greeter::greet(const std::string& name) const {
    return "Hello, " + name + calm();
}
```

**Java — `Greeter.java`, one file, two classes**

```java
public class Greeter implements Greeting {

    @Override
    public String greet(String name) {
        return "Hello, " + name + Punctuation.calm();
    }
}

class Punctuation {

    static String calm() { return "."; }
    static String loud() { return "!"; }
}
```

The ratio inverts. In C++ one class routinely spans two files. In Java **one file can hold
several classes but only one public one**, so a class never spans files.

| C++ | Java |
|---|---|
| `Greeter.h` + `Greeter.cpp` | `Greeter.java` — declaration and body together |
| `#include "Greeting.h"` | nothing. Same package, so the name is enough |
| `: public Greeting` | `implements Greeting` (`extends` is for classes) |
| `override` — optional keyword, checked | `@Override` — optional annotation, checked |
| anonymous namespace for file-local helpers | a non-`public` class in the same file |
| filename is free; `greeter.cpp` is fine | filename **is** the public class name |

`@Override` is worth the same reflex `override` earns you in C++: it turns "I typed the
signature wrong and silently added a new method" into a compile error.

## The second implementation, and why no include appears

**C++ — `Shouter.cpp` must say where `Punctuation` came from**

```cpp
#include "Punctuation.h"   // and Punctuation must have been given a header
```

**Java — `Shouter.java` says nothing**

```java
public class Shouter implements Greeting {

    @Override
    public String greet(String name) {
        return "HELLO " + name.toUpperCase() + Punctuation.loud();
    }
}
```

`Punctuation` is declared in `Greeter.java`, a file with a different name, and `Shouter.java`
references it with no import and no include. Same package, so `javac` already knows it.

This is the load-bearing difference: **in C++ you tell the compiler where a type is; in Java
you tell it the name and it searches.** That search is J2.

## Order, and the forward declaration you keep almost writing

**C++ — mutual reference needs a declaration first**

```cpp
class Punctuation;            // forward declaration, or Greeter will not compile

class Greeter {
    Punctuation* p;
};
```

**Java — no such construct exists**

```java
public class Main {

    public static void main(String[] args) {
        System.out.println(describe(new Greeter(), "Pratyush"));
    }

    // Declared below its caller. Fine.
    static String describe(Greeting greeting, String name) {
        return greeting.getClass().getSimpleName() + " says: " + greeting.greet(name);
    }
}
```

`javac` reads every file you hand it, builds the full set of type and member signatures, and
*then* resolves bodies. So a method may call one declared beneath it, two classes may refer to
each other freely, and the order of files on the command line is meaningless. There is no
`class Punctuation;` syntax to reach for, which is exactly why hunting for it wastes ten minutes
the first time.

## The one C++ habit that will bite today

Naming the file after what it does rather than after the class. `greeter.java`,
`greeting_impl.java`, `types.java` — all fine in C++, all fatal here:

```
greeter.java:1: error: class Greeter is public, should be declared in a file named Greeter.java
```

Read that message as a **rename instruction**, and note it names the file it wanted. It is the
most common Java error a C++ developer produces on day one, and it takes two seconds to fix
once you recognise it.
