1

I am trying to complete the Appendix attached Appendix. I just want to know if I am coding it correctly according to the Appendix and that I am using the correct approach. I am not sure if I did the correct thing under interest(). Where I called the super classes is that correct?

public interface LoanInterest { double interest(); String getName(); String toString(); } //end of LoanInterest 
public abstract class Student implements LoanInterest { private String name; private String studentNumber; private double feesOwed; public Student(String nm, String num, double amt) { name = nm; studentNumber = num; feesOwed = amt; } public double getFeesOwed() { return feesOwed; } public String getStudentNumber() { return studentNumber; } public String getName() { return name; } public String toString() { String msg; msg = String.format("%s\t%s\t%.2f", name, getStudentNumber(), getFeesOwed()); return msg; } } //end of Student public class UnderGrad extends Student { public UnderGrad(String nm, String num, double amt) { super(nm, num, amt); } public double interest() { return super.getFeesOwed() + (super.getFeesOwed() * 0.14); } } //end of UnderGrad public class PostGrad extends Student { private String diploma; public PostGrad(String nm, String num, double amt) { super(nm, num, amt); } public String getDiploma() { return diploma; } public double interest() { return super.getFeesOwed() + (super.getFeesOwed() * 0.14); } } //end of PostGrad 

1 Answer 1

1

You don't need to call super.methodName, since you do not override them in PostGrad or UnderGrad, but it is not "wrong" either.

So you can write:

public double interest() { return getFeesOwed() + (getFeesOwed() * 0.14); } 

And if you would override them, you most likely want to use them too, so again no super.

The super keyword is normally used, if a method is overridden to add some additional functionality to it, without completely rewriting the code of the overridden method.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you.I took out the super. Am I applying or implementing the Interface and abstract correctly? is my classes coded correctly according to the appendix?
It looks correct. You might want to add the @Override annotation to the methods that do that, which is not necessary but a good convention. And you can return the String in the toString method directly, no need to assign it to a local variable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.