How do I override a method in a subclass?

前端 未结 1 1099
被撕碎了的回忆
被撕碎了的回忆 2021-01-07 11:36

I have an inventory program written to include an array and a method to calculate total cost for all inventory items entered. I now have to include a subclass that overrides

相关标签:
1条回答
  • 2021-01-07 12:23

    You simply declare a method with the same signature as the method in the parent class. So yours would look like:

    package inventory3;
    
    public class ItemDetails extends Items {
        private String Name;
        private double pNumber, Units, Price;
    
        public ItemDetails(String Name, double pNumber, double Units, double Price) {
            this.Name = Name;
            this.pNumber = pNumber;
            this.Units = Units;
            this.Price = Price;
        }
    
        // getters and setters....
    
        // The @Override is optional, but recommended.
        @Override
        public double calculateTotalPrice() {
            return Units * Price * 1.05; // From my understanding this is what you want to do
        }
    }
    
    0 讨论(0)
提交回复
热议问题