Post

Created by @nathanedwards
 at October 31st 2023, 12:42:03 pm.

Question:

public class Animal {
  private String name;
  private int age;
  
  public Animal(String name, int age) {
    this.name = name;
    this.age = age;
  }
  
  public String getName() {
    return name;
  }
  
  public int getAge() {
    return age;
  }
  
  public void speak() {
    System.out.println("The animal is making a sound.");
  }
}

public class Dog extends Animal {
  private String breed;
  
  public Dog(String name, int age, String breed) {
    super(name, age);
    this.breed = breed;
  }
  
  public String getBreed() {
    return breed;
  }
  
  public void speak() {
    System.out.println("The dog is barking.");
  }
}

public class Cat extends Animal {
  private String color;
  
  public Cat(String name, int age, String color) {
    super(name, age);
    this.color = color;
  }
  
  public String getColor() {
    return color;
  }
  
  public void speak() {
    System.out.println("The cat is meowing.");
  }
}

public class Main {
  public static void main(String[] args) {
    Animal animal1 = new Dog("Buddy", 5, "Labrador Retriever");
    Animal animal2 = new Cat("Whiskers", 3, "Orange");
    
    // Write code here to display the name, age, breed, and color of animal1 and animal2

    // Write code here to invoke the speak() method for animal1 and animal2
  }
}

In the given code, the classes Animal, Dog, Cat, and Main are defined. The Animal class is the parent class, while the Dog and Cat classes are subclasses of Animal. The Main class contains the main method.

You are required to write code in the Main class to display the name, age, breed, and color of animal1 and animal2, and to invoke the speak() method for both animals.

Answer:

public class Main {
  public static void main(String[] args) {
    Animal animal1 = new Dog("Buddy", 5, "Labrador Retriever");
    Animal animal2 = new Cat("Whiskers", 3, "Orange");
    
    // Displaying information of animal1
    System.out.println("Animal 1:");
    System.out.println("Name: " + animal1.getName());
    System.out.println("Age: " + animal1.getAge());
    System.out.println("Breed: " + ((Dog) animal1).getBreed()); // Casting necessary to access the subclass-specific method
    
    // Displaying information of animal2
    System.out.println("\nAnimal 2:");
    System.out.println("Name: " + animal2.getName());
    System.out.println("Age: " + animal2.getAge());
    System.out.println("Color: " + ((Cat) animal2).getColor()); // Casting necessary to access the subclass-specific method
    
    // Invoking the speak() method for animal1
    System.out.println("\nAnimal 1 speaks:");
    animal1.speak(); // Polymorphic method invocation based on the actual object type
    
    // Invoking the speak() method for animal2
    System.out.println("\nAnimal 2 speaks:");
    animal2.speak(); // Polymorphic method invocation based on the actual object type
  }
}

Explanation:

  1. Firstly, we create two animals: animal1, which is a Dog object with the name "Buddy", age 5, and breed "Labrador Retriever", and animal2, which is a Cat object with the name "Whiskers", age 3, and color "Orange".

  2. To display the information of animal1, we use the getName(), getAge(), and getBreed() methods. Since animal1 is declared as an Animal object, we need to cast it to Dog to access the getBreed() method. The information is then printed using System.out.println().

  3. Likewise, to display the information of animal2, we use the getName(), getAge(), and getColor() methods. Since animal2 is declared as an Animal object, we need to cast it to Cat to access the getColor() method. The information is printed similarly.

  4. To invoke the speak() method for animal1, we simply call animal1.speak(). This will invoke the speak() method overridden in the Dog class, resulting in the phrase "The dog is barking." being printed.

  5. Similarly, to invoke the speak() method for animal2, we call animal2.speak(). This will invoke the speak() method overridden in the Cat class, resulting in the phrase "The cat is meowing." being printed.

Note: Inheritance allows objects of a subclass to be treated as objects of the superclass, which is why animal1 and animal2 can be declared as Animal objects but initialized with Dog and Cat objects, respectively. Polymorphism allows different objects to respond to the same method call in different ways, as demonstrated by the speak() method in the subclasses.