Question

im a programming newbie studying computerscience in second semester. sorry for the german language program. but i hope it won't be a problem understanding it. it basically is about converting currencies.

My question is: what is the function of a class as a variable/data type here? i mean in the class dm (deutsche mark) you have the constructor with the given parameter (Euro a). should i think of "a" as a new object of the class euro? or what is it exactly? i saw this kind of variables in many occasions, apparently it is quite common. i hope someone can help.

thanks!

Mel

public abstract class waehrung {
    public abstract double dollarBetrag();
}   

public class Euro extends waehrung {
    private static double kurs = 1;
    private double wert;

    public Euro(double wert){
        this.wert = wert; 
}

    public double dollarBetrag(){
        return wert*kurs;
    }

    public double euroBetrag(){
    return wert;
    }

    public static void setEuroKurs(double Kurs){
        kurs = kurs;
    }   
}

class DM extends Euro{
    public DM (double dm){      
        super(dm/1.95583);
    }

    public DM(Euro a){  
        super(a.euroBetrag());
    }   

    public double waehrungsBetrag() {
        return euroBetrag()*1.95583;
    }
}
Was it helpful?

Solution 3

Note that DM is a subclass of Euro, and in its constructor

public DM(Euro a){  
    super(a.euroBetrag());
} 

it calls to a.eurobetrag() and pass it to the superclass constructor:

public Euro(double wert){
    this.wert = wert; 
}

therefore, a is not a variable of DM class, it's just used to get a value a.euroBetrag() and then assigns it to the attribute wert:

this.wert = wert; 

OTHER TIPS

You should think of a as an reference to an object of class Euro. It came from the code used to construct the DM object:

DM dm = new DM(euro);

where euro is a reference to some object of class Euro. In the constructor, this becomes a.

Question:

what is the function of a class as a variable/data type here? i mean in the class dm (deutsche mark) you have the constructor with the given parameter (Euro a). should i think of "a" as a new object of the class euro? or what is it exactly? i saw this kind of variables in many occasions, apparently it is quite common

 public DM(Euro a){  
        super(a.euroBetrag());
 }

Answer : a is a variable wich is type is Euro, so a (that is called reference) is referring to an Euro object. An Euro object in your example could be a DM or Euro.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top