Answer to Question #348664 in Java | JSP | JSF for Stephanie

Question #348664

Declare the Person superclass as abstract. Add this abstract method to the class:

public abstract void eat(String food);

Both the Student and Employee class inherit from abstract superclass Person, and provides an implementation for eat() which displays that the student or employee is eating a certain food.

(Note what happens if you don’t implement eat().)



1
Expert's answer
2022-06-07T17:43:22-0400
abstract class Person {
    public abstract void eat(String food);
}

class Student extends Person {
    // If `eat` is not implemented, then a compilation error happens
    public void eat(String food) {
        System.out.println("The student is eating " + food);
    }
}

class Employee extends Person {
    // If `eat` is not implemented, then a compilation error happens
    public void eat(String food) {
        System.out.println("The employee is eating " + food);
    }
}

class Test {
    public static void main(String[] args) {
        Student student = new Student();
        student.eat("cheese");
        Employee employee = new Employee();
        employee.eat("soup");
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS