w3resource

Java: Abstract Person Class with Athlete and LazyPerson Subclasses

Java Abstract Class: Exercise-8 with Solution

Write a Java program to create an abstract class Person with abstract methods eat() and exercise(). Create subclasses Athlete and LazyPerson that extend the Person class and implement the respective methods to describe how each person eats and exercises.

Sample Solution:

Java Code:

//Person.java
abstract class Person {
  public abstract void eat();

  public abstract void exercise();
}

//Athlete.java
class Athlete extends Person {
  @Override
  public void eat() {
    System.out.println("Athlete: Include foods full of calcium, iron, potassium, and fiber.");
  }

  @Override
  public void exercise() {
    System.out.println("Athlete: Training allows the body to gradually build up strength and endurance, improve skill levels and build motivation, ambition and confidence.");
  }
}
//LazyPerson.java
class LazyPerson extends Person {
  @Override
  public void eat() {
    System.out.println("Couch Potato: Eating while watching TV also prolongs the time period that we're eating.");
  }

  @Override
  public void exercise() {
    System.out.println("Couch Potato: Rarely exercising or being physically active.");
  }
}
//Main.java
public class Main {
  public static void main(String[] args) {
    Person athlete = new Athlete();
    Person lazyPerson = new LazyPerson();
    athlete.eat();
    athlete.exercise();
    lazyPerson.eat();
    lazyPerson.exercise();
  }
}

Sample Output:

Athlete: Include foods full of calcium, iron, potassium, and fiber.
Athlete: Training allows the body to gradually build up strength and endurance, improve skill levels and build motivation, ambition and confidence.
Couch Potato: Eating while watching TV also prolongs the time period that we're eating.
Couch Potato: Rarely exercising or being physically active.

Explanation:

In the above exercise -

  • The abstract class "Person" with two abstract methods eat() and exercise(). The subclasses Athlete and CouchPotato extend the Person class and provide their own implementations for these abstract methods.
  • The "Athlete" class describes how an athlete eats a balanced diet and engages in regular intense workouts, while the "LazyPerson" class describes how a couch potato consumes junk food and snacks while rarely exercising or being physically active.
  • In the main method, we create instances of Athlete and LazyPerson, and then call the eat() and exercise() methods on each object to describe the eating and exercising habits of both types of people.

Flowchart:

Flowchart: Person Java
Flowchart: Athlete Java
Flowchart: LazyPerson Java
Flowchart: Main Java

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Abstract Vehicle Class with Car and Motorcycle Subclasses.
Next: Abstract Instrument Class with Glockenspiel and Violin Subclasses.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.