#Couple Questions Regarding abstract classes and IS-A/HAS-A Relationships

1 messages · Page 1 of 1 (latest)

lethal coral
#

I'm currently working on an assignment for an application development course, and I'm still a bit confused on abstract classes and how different relationships work in Java. What I understand so far is that an abstract class is used to store information that can be accessed by other classes, but is not instantiated (am probably butchering my terminology).

Below is an example of an abstract class I've made for an assignment, and I am unsure of how to approach creating classes which will have IS-A relationships. In the chat inside of this post, I'll send the block of code, which is an abstract item class for a program which acts as a POS tool for an imaginary restaurant. In this case, I'll need to create specialized classes for food and beverages.

Grateful if anyone can give me some guidance on how to approach this (Not looking for someone to give me the answer, I'd like to actually learn this.)
I have also attached some guidlines for this in case it provides any extra insight.
I will send the code after this is posted as I am breaking the 2,000 character limit and unfortunately do nt have nitro.

ornate pumiceBOT
#

<@&987246399047479336> please have a look, thanks.

lethal coral
#

public abstract class Item {
    
    private String name;
    private String description;
    private double price;
    private boolean onSpecial;
        private boolean isBeverage;
    
    public Item(){
        name = "";
        description = "";
        price = 0.0;
        onSpecial = false;
        isBeverage = false;
    }
    
    public Item(String aName, String aDescription, double aPrice){
        setName(aName);
        setDescription(aDescription);
        setPrice(aPrice);
        onSpecial = false;
        isBeverage = false;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public double getPrice() {
        return price;
    }

    public boolean isOnSpecial() {
        return onSpecial;
    }

    public boolean isBeverage() {
        return isBeverage;
    }

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

    public void setDescription(String description) {
        this.description = description;
    }

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

    public void setOnSpecial(boolean onSpecial) {
        this.onSpecial = onSpecial;
    }

    public void setBeverage(boolean isBeverage) {
        this.isBeverage = isBeverage;
    }
    
    public void noLongerOnSpecial(){
        onSpecial = false;
    }

    @Override
    public String toString() {
        return "I'm an Item [name=" + name + ", description=" + description + ", price=" + price + ", onSpecial=" + onSpecial
                + "]";
    }

}
ornate pumiceBOT
# lethal coral ```java public abstract class Item { private String name; private ...

Detected code, here are some useful tools:

Formatted code
public abstract class Item {
  private String name;
  private String description;
  private double price;
  private boolean onSpecial;
  private boolean isBeverage;
  public Item() {
    name = "";
    description = "";
    price = 0.0;
    onSpecial = false;
    isBeverage = false;
  }
  public Item(String aName, String aDescription, double aPrice) {
    setName(aName);
    setDescription(aDescription);
    setPrice(aPrice);
    onSpecial = false;
    isBeverage = false;
  }
  public String getName() {
    return name;
  }
  public String getDescription() {
    return description;
  }
  public double getPrice() {
    return price;
  }
  public boolean isOnSpecial() {
    return onSpecial;
  }
  public boolean isBeverage() {
    return isBeverage;
  }
  public void setName(String name) {
    this .name = name;
  }
  public void setDescription(String description) {
    this .description = description;
  }
  public void setPrice(double price) {
    this .price = price;
  }
  public void setOnSpecial(boolean onSpecial) {
    this .onSpecial = onSpecial;
  }
  public void setBeverage(boolean isBeverage) {
    this .isBeverage = isBeverage;
  }
  public void noLongerOnSpecial() {
    onSpecial = false;
  }
  @Override
  public String toString() {
    return "I'm an Item [name=" + name + ", description=" + description + ", price=" + price + ", onSpecial=" + onSpecial + "]";
  }
}
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

cursive sun
#

here is a good explanation about abstract classes and their use case:

ornate pumiceBOT
#

An abstract class denotes a yet unfinished class. For example, it might be only done to 70% but there are still 30% missing. As such, it is used to create a template for other classes that extend it. The extending class can then concentrate on only doing the remaining 30% since 70% are already done. This can greatly reduce code duplication.

In general, a class is abstract if it has an abstract method. An abstract method is a method that is missing any implementation:

abstract class Animal {
  String name;

  Animal(String name) {
    this.name = name;
  }

  String getName() { // regular method
    return name;
  }

  abstract void makeNoise(); // No method body
}

Since the class is not finished yet and makeNoise is lacking any implementation, it is impossible to create instances of it in that state:

Animal animal = new Animal("Buddy"); // Does not compile! Can not instantiate abstract class

However, we can use it as template for extending it and get the existing name-functionality for free (getName()). But then we are required to implement everything that is still abstract, i.e. incomplete like makeNoise:

class Dog extends Animal {
  Dog() {
    super("Buddy"); // all dogs are called Buddy
  }

  @Override
  void makeNoise() {
    System.out.println("Wuff Wuff");
  }
}

And now we can create instances of it and use the methods:

Dog dog = new Dog();
System.out.println(dog.getName()); // Prints Buddy
dog.makeNoise(); // Prints Wuff Wuff
cursive sun
#

regarding is-a vs has-a:

  • is-a: a Dog is an Animal. but an Eye is not a Person. is-a usually refers to inheritance (Foo extends Bar)
  • has-a: a Person has an Eye (two even). usually refers to fields/members/properties of a class. also called composition
#

in ur specific case, an Item has a name

#

and Food is an Item

#

they want u to make Item as abstract base class for Food and Beverage, which then both extend Item