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.
SomeClasslikeprivate SomeClass variable = new SomeClass(...);BigDecimalor a whole number of cents stored in along.