noor
noor

Reputation: 21

interface,abstract and classes

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

Upvotes: 1

Views: 59

Answers (1)

Pinkie Swirl
Pinkie Swirl

Reputation: 2415

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.

Upvotes: 1

Related Questions