简体   繁体   中英

How to inherit interface from superclass

I have an interface with a superclass, a subclass, and a driver class. The interface must be implemented in the subclass, however, I am confused on how to do it. Do I implement the interface in the superclass and then extends the subclass? The superclass is called store.

The subclass is called retail, it should receive the superclass's constructor, the number of items sold, unit price, and sale price should be array arguments. This class implements two methods that are defined at interface.

The interface should have two methods. One is the profit and the other is salary The profit method is to calculate the store profit in a week, and salary method is to calculate the store manager's salary in a week.

/*
The interface should have two methods.
One is the “Profit” and the other is “Salary”.
The profit method is to calculate the store profit in a week, and salary         method
is to calculate the store manager’s salary in a week.
*/
public interface Interface {

    public void profit();
    public void salary();

}
/*
The store class is a super class that receives store location, 
manager name, hours worked, and hour rate.  
This class should have the constructor that receives all of these.  
It also should have get and set methods for each of fields. 
This class also has “toString()” to 
display restaurant location and manager’s name.
*/
public class Store {
    private String location;
    private String manager;
    private int hours;
    private int rate;

    public Store(String l, String m, int hrs, int r) {
        location = l;
        manager = m;
        hours = hrs;
        rate = r;
    }

    public void setLocation(String l) {
        location = l;
    }

    public String getLocation() {
        return location;
    }

    public void setName(String m) {
        manager = m;
    }

    public String getName() {
        return manager;
    }

    public void setHours(int hrs) {
        hours = hrs;
    }

    public int getHours() {
        return hours;
    }

    public void setRate(int r) {
        rate = r;
    }

    public int getRate() {
        return rate;
    }

    public String toString() {
        String result = "Store Location: " + location + "\n";
        result += "Manager name:" + manager + "\n";
        return result;
    }
}
public class Retail extends Store {
    private int items;
    private double unit;
    private double sale;

    public Retail(String l, String m, int hrs, int r,int i, double u, double s){
        super(l,m, hrs, r);
        items = i;
        unit = u;
        sale = s;
    }
    public void profit() {
        double[][] money = {{1.99, 2.99, 3.99, 4.99},
                            {5.99, 6.99, 7.99, 8.99},
                            {150, 48, 350,20}};
        for (int i = 0; i < money.length; i++) {
            for (int j = 0; j < money[i].length; j++) {
                sum += money[i][j];
            }
        }
        double profit = items * ( s - u);
    }

    public void salary() {
        double pay = hrs * r;
        //double salary = pay - ( pay * 0.05);
    }

    public double getSalary() {
        double baseSalary = super.getHours();
    }

    public String toString() {
        result += super.getName(); // inherited from superclass
        String result = "Total Benefit: " + profit + "\n";
        result += "Salary: " + salary + "\n";
        return result;
    }
}

General rules:

  • If the interface contract is applicable in the full hierarchy then implement it in the superclass and all subclasses adhere to the interface contract automatically.
  • You may choose to implement the interface in the superclass but make the superclass abstract if it does not have enough details to implement the interface methods. This way you can enforce the subclasses to adhere to the interface contract.
  • If the interface contract is not at all relevant to the full hierarchy then implement only in the applicable subclasses.

Your case:

In your example, the question is whether interface methods profit() and salary() applicable to any kind of Store ? If yes (I assume it is), then go ahead and implement in the superclass. However, you may not be able to compute profit() and salary() in the Store class with the data points available. So, you may choose to declare Store class as abstract. In case you can implement these methods make Store class concrete.

On the other hand, if the interface methods profit() and salary() may not be applicable to all kind of Store s then go ahead and implement the interface only in Retail class.

Though I think the first option is the good one however, the choice is yours based on the business scenario.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM