Reputation: 1658
I am trying to understand how interfaces work. I have read basic interface tutorials online and watched a few videos so i do have a good idea of what a interface is and its advantages. However i came across this piece of code that is utilizing interface feature and am having trouble getting my head around some of it. So i needed someone to just breakdown what exactly is happening. I will also write down what i understand of it so far so if i am wrong you can correct me.
Here is the Code.
Bank Account Class
package Interface;
public class BankAccount implements Measurable {
private double balance;
public BankAccount() {
balance = 0;
}
public BankAccount(double initialBalance) {
balance = initialBalance;
}
public void deposit(double amount) {
double newBalance = balance + amount;
balance = newBalance;
}
public void withdraw(double amount) {
double newBalance = balance - amount;
balance = newBalance;
}
public double getBalance() {
return balance;
}
public double getMeasure() {
return balance;
}
}
Coin Class
package Interface;
public class Coin implements Measurable {
private double value;
private String name;
public Coin(double aValue, String aName) {
value = aValue;
name = aName;
}
public double getValue() {
return value;
}
public String getName() {
return name;
}
public double getMeasure() {
return value;
}
}
Interface
package Interface;
public interface Measurable {
double getMeasure();
}
Data Set Class
package Interface;
public class DataSet {
private double sum;
private Measurable maximum;
private int count;
public DataSet() {
sum = 0;
count = 0;
maximum = null;
}
public void add(Measurable x) {
sum = sum + x.getMeasure();
if (count == 0 || maximum.getMeasure() < x.getMeasure())
maximum = x;
count++;
}
public double getAverage() {
if (count == 0)
return 0;
else
return sum / count;
}
public Measurable getMaximum() {
return maximum;
}
}
Test Class
package Interface;
public class Test {
public static void main(String[] args) {
DataSet bankData = new DataSet();
bankData.add(new BankAccount(100));
bankData.add(new BankAccount(100000));
bankData.add(new BankAccount(10));
Measurable max = bankData.getMaximum();
System.out.println("Maximum :" + max.getMeasure());
DataSet coinData = new DataSet();
coinData.add(new Coin(0.25, "Quarter"));
coinData.add(new Coin(0.10, "Dime"));
coinData.add(new Coin(0.05, "Nickel"));
max = coinData.getMaximum();
System.out.println("Maximum : " + max.getMeasure());
}
}
What i understand so far
I completely understand the bank class, the coin class and the interface. My problem is i don't quite understand a few things in the DataSet Class. First of all i do not get
the use of private Measurable maximum;
My understanding is after the specifier you usually declare a variable type such as int or double but in this case its assigning a variable name to the interface. I guess if someone can explain me this then it will help me clarify my understanding of the DataSet class methods like its add method where Measurable parameter is passed.
Lastly i have difficulty understanding the Test class. I don't get how bankData.add(new BankAccount(100));
quite works. From what i understand we created a new object of dataset type but i don't get how a new bankaccount object is being created inside it.
If someone can please help me clarify this it would really help me understand interfaces a lot better. i have looked at a lot of simple examples that show very basic code like a car interface with a few methods which are then implemented by specific cars. However those primitive examples don't really help one in understanding the true essence of a interface. I believe this example is more practical and it will help. I apologize if this question is poorly worded as i am new to this java.
Upvotes: 0
Views: 3300
Reputation: 10945
To answer your first question, the code private Measurable maximum;
is simply declaring a variable named maximum
which is of type Measurable
. What this means is that any object whose class implements the Measureable
interface can be assigned as a value for the variable.
To answer your second question, any time you see the new keyword, an object is being created, so when you see this:
DataSet bankData = new DataSet();
bankData.add(new BankAccount(100));
...the first line is creating a new DataSet
object, and the second is running the add()
method on that object, and passing in as an argument a new BankAccount
object.
In this case, the new object hasn't been assigned to a variable in the main
method, since we don't have any use for it apart from passing it in to the BankAccount
.
So, regarding this bit of code:
Measurable max = bankData.getMaximum(); // get the max being store by the bank data
System.out.println("Maximum :" + max.getMeasure()); // run the appropriate getMeasure method, depending on the actual type of max
What's going on here is that we first want to get the maximum from the bankData
object, and then run the getMeasure()
method on it. Note that the type of the max
variable is Measureable
, just like the type of the argument to the add()
method above it.
When we are adding Measureable
s to our bank data, we don't know the true type, just that it's some class which has implemented the appropriate interface:
public void add(Measurable x) {
sum = sum + x.getMeasure();
if (count == 0 || maximum.getMeasure() < x.getMeasure())
maximum = x; // here maximum gets set to the object passed in, but we don't know its type at this point
count++;
}
When we get max
out of our bankData
, we can run getMeasure()
on it, even though we don't know the type of the object at compile time (this is something referred to as polymorphism).
Depending on the actual type of the object, the appropriate getMeasure()
method will be called. In this case, since we add()
ed BankAccount
s, then BankAccount
's method will get called. But if we had added Coin
s instead, then the getMeasure()
method of the Coin
class would have been called.
Upvotes: 3
Reputation: 5093
First question:
In terms of variables and parameters, an interface can be used like any other class. Think of an interface an a contract. Declaring a variable of the interface type is like saying "whatever is in this pointer will fulfill this contract". You don't really care if it is a coin or a bank account, as long as it fulfills the Measurable interface.
Second question:
bankData.add(new BankAccount(100));
is similar to
BankAccount newAccount=new BankAccount(100);
bankData.add(newAccount);
now lets add the implict cast
BankAccount newAccount=new BankAccount(100);
Mesurable bankAccountAsMeasurable = (Mesuarable)newAccount;
bankData.add(bankAccountAsMeasurable);
Does that clear things up?
Upvotes: 1