public DM(Euro a){
a
isn't a "new" object. When the rest of the program uses this constructor, it will need to have an existing Euro
object, like this:
Euro someEuroVariable; // for example
...
DM d = new DM(someEuroVariable);
and the constructor will then use someEuroVariable
as its parameter, which it refers to as a
. But no new Euro
object is created by this.
Hope this helps.
EDIT: To answer the question in the comments: You can't say
DM d = new DM();
because DM
doesn't have a constructor that you can use with no parameters. But let's say you used a legal constructor like
DM d = new DM(1.234);
and now you say
Euro a = d;
a
and d
will be references to the same object. This object has a run-time type of DM
. When you declare a variable a
to have type Euro
, that means that a
can, during its lifetime, be an object of type Euro
or any of its subclasses. However, since the compiler can't tell what the actual type will be at runtime, you can only use a method a.method()
if the method was defined for Euro
. If DM
has a new method method2
that Euro
doesn't have, you can't call it directly with a.method2()
. It also means that later on, you could reassign a
to an object with some other subclass:
a = new Lira(100000000000.0);
while you can't do that with d
because it can only be of type DM
or a subclass of DM
:
d = new Lira(100000000000.0); // Error at compile time
This distinction between compile-time type and run-time type can be difficult to grasp until you get used to it. It's also totally unrelated to what happens when you pass a Euro
as a parameter to a DM
constructor; the object that you pass as a parameter is a separate object and must exist before the constructor is called.