-4

I keep getting the error

constructor product in class product cannot be applied to given types

In the super(name, price, barcode) section.

Here is my code :

Product class:

public class Product {


    public String name;
    public float price;
    public int barCode;



  public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

     public float getPrice() {
        return price;
    }
    public void setPrice(float price) {
        this.price = price;
    }

     public int getBarCode() {
        return barCode;
    }
    public void setBarCode(int barCode) {
        this.barCode = barCode;
    }



    public String tostring(){
        return (" Name: " +name+ ",  Price: " +price+ ", Barcode no: " +barCode);
    }

}

Chocolate class:

public class Chocolate extends Product{

    public Chocolate(String name, float price, int barcode) {
        super(name, price, barcode);
    }

    @Override
    public String toString() {
        return "Decs : Name :"+super.getName()+", price :"+super.getPrice()+", barcode : "+super.getBarCode();
    }
}

Whine class:

public class Whine  extends Product {

    public Whine(String name, float price, int barcode) {
        super(name, price, barcode);
    }

    @Override
    public String toString() {
        return "Decs : Name :"+super.getName()+", price :"+super.getPrice()+", barcode : "+super.getBarCode();
    }

}

Main method:

public static void main(String[] args) {
        Chocolate c1 = new Chocolate("abcd1", "c1", 23.7d, 2.3d);
        Chocolate c2 = new Chocolate("abcd2", "c2", 23d, 2.3d);
        Chocolate c3 = new Chocolate("abcd3", "c3", 23.2d, 2.3d);
        Chocolate c4 = new Chocolate("abcd4", "c4", 23.1d, 2.3d);

        Whine w1 =  new Whine("Whine1", "w1", 23.7d, 2.3d);
        Whine w2 =  new Whine("Whine2", "w2", 233.7d, 22.3d);

        System.out.println("C1"+c1);
        System.out.println("C2"+c2);
        System.out.println("C3"+c3);
        System.out.println("C4"+c4);

        System.out.println("W1"+w1);
        System.out.println("W2"+w2);
    }

}
Sam
  • 7,252
  • 16
  • 46
  • 65
Garcia1987
  • 13
  • 2

4 Answers4

3

Well, Product does not have the constructor you’re calling.

Bombe
  • 81,643
  • 20
  • 123
  • 127
3

You need to create your constructor on your Product class:

Product(String name, float price, int barcode) {
    //add your code here
}

Because otherwise Java creates a default constructor like this:

Product() {
    //add your code here
}

And then you get your conflict because of parameter length on both constructors.

To solve it, you need to:

  1. Create your own constructor as I wrote above

OR

  1. Call super() to call the default Java constructor instead of super(name, price, barcode).

And as @chrylis stated in his comment you're overriding tostring() method, so you should add an @Override tag above it, so it's easier to read for you and others, as you did on your Chocolate class:

@Override
public String tostring(){
    return (" Name: " +name+ ",  Price: " +price+ ", Barcode no: " +barCode);
}
Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89
2

you are calling in your sub classes super(name, price, barcode);. In java this tries to call the constructor of your parent class that excepts these three params, which doesn't exist.

You just need to use the default super constructor (super()) or add the constructor to Product (which is better):

public Product(String name, float price, int barcode) {
  this.name = name;
  this.price = price;
  this.barcode = barcode;
}
Nir Levy
  • 12,750
  • 3
  • 21
  • 38
1

add appropriate constructor to you Product class and declare it (maybe) as an abstract class:

public abstract class Product {

/* ... your properties ... */

public Product(String name, float price, int barCode) {
    this.name = name;
    this.price = price;
    this.barCode = barCode;
}

/* etc ... */
}

But, a better idea is to make it as an Interface:

public interface Product {
    String getName();
    BigDecimal getPrice();
    int getBarCode();
    /* etc */
}

public class Chocolate implements Product {
    private String name;
    private BigDecimal price;
    private int barCode;

    public Chocolate(String name, BigDecimal price, int barCode) {
        this.name = name;
        this.price = price;
        this.barCode = barCode;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public BigDecimal getPrice() {
        return price;
    }

    @Override
    public int getBarCode() {
        return barCode;
    }
}

Then you can talk about polymorphism.

public static class main(String args[]) {
    Product chocolate = new Chocolate("Neuhaus", new BigDecimal(7.38), 9785455);
}
LowLevel
  • 1,085
  • 1
  • 13
  • 34
  • now getting error in main method : cannot find symbol. symbol: class Chocolate – Garcia1987 Dec 22 '15 at 20:00
  • I made a mistake within class `Chocolate`. I've already edited my answer. It must be `public int getBarCode()` instead of `public String getBarCode()` – LowLevel Dec 22 '15 at 20:07
  • @Garcia1987 If you have new problem then ask new question (you can provide link to old question in it). But to be honest before you post new question you should first search for potential solutions on Stack Overflow or other sites. This way you could find posts like [What does a “Cannot find symbol” compilation error mean?](http://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean) – Pshemo Dec 22 '15 at 20:16