Answer to Question #348995 in Java | JSP | JSF for stephanie

Question #348995

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-08T14:27:34-0400
abstract class Person {
    public abstract void eat(String food);
}

class Student extends Person {
    // If we don't implement `eat` method, a compilation error happens
    @Override
    public void eat(String food) {
        System.out.println("The student is eating " + food);
    }
}

class Employee extends Person {
    // If we don't implement `eat` method, a compilation error happens
    @Override
    public void eat(String food) {
        System.out.println("The employee is eating " + food);
    }
}

class Main {
    public static void main(String[] args) {
        Student student = new Student();
        student.eat("sandwich");
        Employee employee = new Employee();
        employee.eat("banana");
    }
}

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
APPROVED BY CLIENTS