Alright, this is for a project designed to make us use inheritance. This is Creature, which is a subclass of Thing. Creature is supposed to be able to eat, move and tell what it ate. Here are my issues:
1. How do I use a constructor from Thing in Creature?
2. How do I get the whatDidYouEat method to get the info from the eat method? What I've done with my lastEaten and aThing vars doesn't work.
3. How do I get whatDidYouEat to check to see if the creature has eaten?

public abstract class Creature extends Thing{
    String lastEaten;
    /** eat method */
    public void eat(Thing aThing){
        System.out.println(super.toString() + " has just eaten a " + aThing);
        lastEaten = aThing;
    }

    /** move method */
    public abstract void move();

    /** what did you eat method*/
    public void whatDidYouEat(){
        if(lastEaten.equals(aThing)){
            System.out.println(super.toString() + " has eaten a " + lastEaten);
        }
        else{
            System.out.println(super.toString() + "has had nothing to eat!");
        }
    }
}

Recommended Answers

All 3 Replies

First you need a constructor for creature, then call your super class in the the creature's constructor.

Alright, this is for a project designed to make us use inheritance. This is Creature, which is a subclass of Thing. Creature is supposed to be able to eat, move and tell what it ate. Here are my issues:
1. How do I use a constructor from Thing in Creature?
2. How do I get the whatDidYouEat method to get the info from the eat method? What I've done with my lastEaten and aThing vars doesn't work.
3. How do I get whatDidYouEat to check to see if the creature has eaten?

public abstract class Creature extends Thing{
    String lastEaten;
    /** eat method */
    public void eat(Thing aThing){
        System.out.println(super.toString() + " has just eaten a " + aThing);
        lastEaten = aThing;
    }

    /** move method */
    public abstract void move();

    /** what did you eat method*/
    public void whatDidYouEat(){
        if(lastEaten.equals(aThing)){
            System.out.println(super.toString() + " has eaten a " + lastEaten);
        }
        else{
            System.out.println(super.toString() + "has had nothing to eat!");
        }
    }
}

First you need a constructor for creature, then call your super class in the the creature's constructor.

... whats the correct code for that? I quickly discovered the wrong code, as shown below.

public abstract class Creature extends Thing{
    Creature(String name){
        super.Thing(String name); //WRONG
    }
    String lastEaten;
    /** eat method */
    public void eat(Thing aThing){
        System.out.println(super.toString() + " has just eaten a " + aThing);
        lastEaten = aThing;
    }

    /** move method */
    public abstract void move();

    /** what did you eat method*/
    public void whatDidYouEat(){
        if(lastEaten.equals(aThing)){
            System.out.println(super.toString() + " has eaten a " + lastEaten);
        }
        else{
            System.out.println(super.toString() + "has had nothing to eat!");
        }
    }
}

Maybe you should google java constructor, and then google java call super class :) you need to understand it rather than just realizing that it works and moving on.

... whats the correct code for that? I quickly discovered the wrong code, as shown below.

public abstract class Creature extends Thing{
    Creature(String name){
        super.Thing(String name); //WRONG
    }
    String lastEaten;
    /** eat method */
    public void eat(Thing aThing){
        System.out.println(super.toString() + " has just eaten a " + aThing);
        lastEaten = aThing;
    }

    /** move method */
    public abstract void move();

    /** what did you eat method*/
    public void whatDidYouEat(){
        if(lastEaten.equals(aThing)){
            System.out.println(super.toString() + " has eaten a " + lastEaten);
        }
        else{
            System.out.println(super.toString() + "has had nothing to eat!");
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.