Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In the example below, the Student class overrides the getFood() method of the Person() class, and it uses super.getFood() to call the Person getFood() method

In the example below, the Student class overrides the getFood() method of the Person() class, and it uses super.getFood() to call the Person getFood() method before adding on to it. Here, a Person is associated with the food "Hamburger" and a Student is associated with "Hamburger" and "Taco".

Add another subclass called Vegan that inherits from the Student class. Add a Vegan contructor that takes a name as an argument and passes it to the super constructor. Override the getFood() method in Vegan to call the superclass getFood() but add a "No " in front of it and then say "but " and add a vegan food. Change Javier to a Vegan object in main() and try it out!

 

public class Person

{

      private String name = null;


 

      public Person(String theName)

      {

         name = theName;

      }


 

      public String getFood()

      {

         return "Hamburger";

      }


 

      public static void main(String[] args)

      {

         Person p = new Student("Javier");

         System.out.println(p.getFood());

      }

   }


 

   class Student extends Person

   {

      private int id;

      private static int nextId = 0;


 

      public Student(String theName)

      {

        super(theName);

        id = nextId;

        nextId++;

      }


 

      public String getFood()

      {

         String output = super.getFood();

         return output + " and Pizza";

      }


 

      public int getId() {return this.id;}

      public void setId (int theId)

      {

         this.id = theId;

      }

   }

   

Step by Step Solution

3.32 Rating (152 Votes )

There are 3 Steps involved in it

Step: 1

public class Person private String name null public PersonString th... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Java An Introduction To Problem Solving And Programming

Authors: Walter Savitch

8th Edition

0134462033, 978-0134462035

More Books

Students also viewed these Programming questions

Question

Describe the goal of cognitive psychotherapy.

Answered: 1 week ago

Question

Detailed note on the contributions of F.W.Taylor

Answered: 1 week ago