I’m trying to implement the UML diagram below using java:
I only got recently exposed to UMLs and according to my understanding, I cannot set a field inside SBank or PBank to define their rate of interest. It can also be seen that in the class Bank there is no implementation for defining the rate of interest like using a setter method. I am looking for advice on how to go about this, or is there something wrong with the given UML? Below are some sample code I used to try and implement the UML into code:
public class BankInfo { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } public class Bank { private BankInfo info; private int rateOfInterest; public Bank(BankInfo info) { this.info = info; } public void displayInterestRate(){ } public int getRateOfInterest(){ return rateOfInterest; } } public class SBank extends Bank{ private int rateOfInterest = 8; public SBank(BankInfo info) { super(info); } @Override public int getRateOfInterest() { return rateOfInterest; } }
Advertisement
Answer
This UML:
Does not show any rateOfInterest
field in any class. What it shows is Bank
is abstract and has an int
-returning abstract method: getRateOfInterest()
. You can tell the class and method are abstract because their names are italicized.
public abstract class Bank { // other members... public abstract int getRateOfInterest(); }
The UML further shows you need to override getRateOfInterest()
in both SBank
and PBank
, both of which extend Bank
, and return that implementation’s rate of interest. You will be returning the value directly rather than storing it in a field. For example, SBank
would look like:
public class SBank extends Bank { public SBank(BankInfo info) { super(info); } @Override public int getRateOfInterest() { return 8; } }
I got the value of 8
from the UML diagram which states:
Interest Rate of SBank is 8 %.