0

I have a product. I am using a previous class, as the only difference between prior and now, is that there is the option of a 10% discount (float 0.1) which can be multiplied by the price, if the order quantity exceeds 10 units.

I extended the Product class using DisountedProduct as the inherited class, but when I call the constructor from the parent class, I get the error "Invalid method declaration, return type required."

I have read the java explanation of inheritance but It doesn't mention this.

Please help

public class DiscountedProduct extends Product {

private float discount;
private String name;
private double price;
private int quantity;

public Product(String name, double price, int quantity){
    this.name = name;
    this.price = price;
    this.quantity = quantity;
}

//Get//

public float getDiscount(){
    return discount;
}

//Set//

public void setDiscount(float value){
    this.discount = value;
}
}


    public class Product {

private String name;
private double price;
private int quantity;

public Product(String name, double price, int quantity){
    this.name = name;
    this.price = price;
    this.quantity = quantity;
}

//Get Methods//

public String getName(){
    return name;
}

public double getPrice(){
    return price;
}

public int getQuantity(){
    return quantity;
}

public double getTotalPrice(){
    return quantity * price;
}

//Set Methods//
public void setName(String value){
    this.name = value;
}

public void setPrice(double value){
    this.price = value;
}

public void setQuantity(int value){
    this.quantity = value;
}
} 

3 Answers 3

6

In DiscountedProduct, replace

public Product(String name, double price, int quantity)

with

public DiscountedProduct(String name, double price, int quantity)

You have the constructor for the wrong class

Also, you don't need to set all the variables in your DiscountedProduct constructor, just call the super constructor; replace the entire method with

public DiscountedProduct(String name, double price, int quantity) {
    super(name, price, quantity);

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

2 Comments

Fixed, got confused with c#
Thanks guys. massive help
0

The constructor in the DiscountedProduct class should be DiscountedProduct instead of Product.

Also, DiscountedProduct should not define the fields already defined in the parent, and constructor should call super(name, price, quantity) instead of initializing the fields itself.

Comments

0

Your Product class is set up fine - the problem seems to be in the constructor for your DiscountedProduct class. The constructor for the DiscountedProduct class should have the same name as the class name: DiscountedProduct. Additionally, the object parameters like name and price are already defined by the parent class, Product. The super() method invokes the Product class constructor. You need to call the super() method and pass it the values used to instantiate a DiscountedProduct.

public DiscountedProduct(String name, double price, int quantity){
    super(name, price, quantity);
}

The fixed constructor should look like the above block. Had no problems with it on my machine.

If you wanted the DiscountedProduct to have a parameter that its parent class does not have, you would define it in its constructor and pass in the corresponding argument, like so:

public DiscountedProduct(String name, double price, int quantity, float defaultDiscount){
        super(name, price, quantity);
        this.discount = defaultDiscount;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.