Produces
Produces

Reputation: 31

Object is not abstract and does not override abstract method

Apologies if the question has been asked before, though I did look around and couldn't find an applicable answer to my specific problem. Anyway, I'm trying to model a store that sells desserts which are all derived from a specific Dessert superclass.

When I try to run the program to test my classes, I get an error say "Cookie is not abstract and does not abstract method getPrice() in Dessert public class Cookie extends Dessert. I am getting the same error with another class called Fruit, but it is more or less the exact same as Cookie just with some different member variables and method names.

Here is the Dessert superclass:

public abstract class Dessert {

    /** Name of the dessert item. */
    protected final String name;

    /**
     * Constructs a new dessert item.
     * @param name Name of the dessert.
     */
    public Dessert(String name) {
        this.name = name;
    }

    /**
     * Get name of the dessert.
     * @return dessert name
     */
    public final String getName() {
        return name;
    }

    /**
     * Get the price of the dessert.
     * @return Dessert price
     */
    public abstract double getPrice();
}

And here is the Cookie subclass:

public class Cookie extends Dessert {
    private int number;
    private double price;

    public Cookie (String name, int number, double price) {
        super(name);
        this.number = number;
        this.price = price;
    }

    public int getItemCount() {
        return number;
    }

    public double getPricePerDozen() {
        return (price / 12) * number;
    }
} 

I can't seem to get the formatting right, but the lines immediately following the colons should be a part of the code block. As well as the curly braces following the block.

Thanks in advance for the help!

Upvotes: 0

Views: 2728

Answers (2)

Laurence Leff
Laurence Leff

Reputation: 76

Since Cookie.java extends Dessert.java and the latter has an abstract method getPrice() Cookie.java must provide a definition for it. (Unless it itself is abstract. One would do that if we had things such as OatmealCookie ChocolateChipCookie that would be the definite class.)

I retyped the material quickly due to the problems git markdown and got these to compile. I assume that the price for Cookie should simply be the price per dozen and added the appropriate definition to it:

public abstract class Dessert {

protected final String name;

public Dessert (String name) {
this.name = name;
}

public final String getName() {
  return name;
}
public abstract double getPrice ();
}



public class Cookie extends Dessert {
private int number;
private double price;

public Cookie (String name, int number, double price) {
  super (name);
  this.number = number;
  this.price = price;
}

public int getItemCount() {
  return number;
}

public double getPricePerDozen() {
  return (price/12) * number;
}

public double getPrice() {
  return getPricePerDozen();
}
}

Upvotes: 1

Elenchus
Elenchus

Reputation: 562

Your Dessert class has an abstract method double getPrice(), and Cookie extends it, so Cookie needs to implement getPrice() or also be abstract in order to get rid of this error.

The code obviously doesn't compile in its current state, but think of it this way - If we were to instantiate a Cookie, its double getPrice() method is inherited from its super class Dessert, so the method would exist to be called, but it has no implementation in either Cookie or Dessert, so the result of calling it would be unspecified. Java sees this at compilation time, and so prevents you from trying to generate code that is ill-defined.

Upvotes: 1

Related Questions