Question

In: Computer Science

Assume that Animal is a class that has method void info() that prints "I am an...

Assume that Animal is a class that has method void info() that prints "I am an animal". Classes Bird and Reptile both extend class Animal, and both override method info(). Class Bird implements method info to print "I am a bird", and class Reptile implements method info to print "I am a reptile ". Determine the outcome printed by the following lines of code.

Animal animal = new Animal();

animal.info(); //OUTCOME:

animal = new Reptile()

animal.info(); //OUTCOME:

animal = new Bird();

animal.info(); //OUTCOME:

Solutions

Expert Solution

class Animal {

  public void info() {
    System.out.println("I am an animal.");
  }

}

class Bird extends Animal {
  public void info() {
    System.out.println("I am a bird.");
  }
}

class Reptile extends Animal {
  public void info() {
    System.out.println("I am a reptile.");
  }
}

class Main {
  public static void main(String[] args) {

    Animal animal = new Animal();

    animal.info();

    animal = new Reptile();

    animal.info();

    animal = new Bird();

    animal.info();
  }
}

The implements keyword is used to implement an interface. The interface keyword is used to declare a special type of class that only contains abstract methods. To access the interface methods, the interface must be "implemented" (kinda like inherited) by another class with the implements keyword (instead of extends). The body of the interface method is provided by the "implement" class.

Therefore,

The first two lines print "I am an animal" as the outcome.

The next two lines print "I am a reptile" as the outcome.

The last two lines print "I am a bird" as the outcome.

I have attached the output below:


Related Solutions

I am stuck on this Java problem: Create an Animal class with: Attributes Age Rabies Vaccination...
I am stuck on this Java problem: Create an Animal class with: Attributes Age Rabies Vaccination Status Name Owner Name A constructor to set values Getters and Setters for all private attributes A toString method that gives the data of all attributes Create a Dog Class Attributes Distemper Vaccination Status A constructor to set values in sub class A constructor to set values in sub and super class Getters and Setters for private attributes A toString method that overrides the...
Animal class Create a simple class called Animal instantiated with a name and a method toString...
Animal class Create a simple class called Animal instantiated with a name and a method toString which returns the name. Cat class Create a simple class Cat which extends Animal, but adds no new instance variable or methods. RedCat class Create a simple class RedCat which extends Cat, but adds no new instance variable or methods. WildCardTester class Create a class with the main method and methods addCat, deleteCat and printAll as follows. addCat method Has two parameters, an ArrayList...
I am struggling with this java code ublic class HW2_Q4 {    public static void main(String[]...
I am struggling with this java code ublic class HW2_Q4 {    public static void main(String[] args) {        // Define the input string. Note that I could have written it all on a        // single line, but I broke it up to match the question.        String input = "Book , Cost , Number\n"                    + "Hamlet , 24.95 , 10\n"                    + "King Lear , 18.42...
I am so lost on this. I am making a B in this class but I...
I am so lost on this. I am making a B in this class but I can not seem to get this project completed. Please help. ACC-120 Project – Financial Accounting This assignment supports the following outcomes: ·          Prepare journal and ledger entries for a service or merchandising business. ·          Prepare year-end statements for a service or merchandising business. ·          Report cost decisions using accounting principles and financial statement analysis. ·          Evaluate how knowledge, skills, and attitudes learned in this...
A JavaFX UI class has a button named 'btnOk' that needs to call a void method...
A JavaFX UI class has a button named 'btnOk' that needs to call a void method with no parameters named 'displayResults' when it is clicked. Write the complete lambda-expression statement to set up the btnOk event handler to do this.
In C, is "&" a operator or punctuator? (According to the info found online, I am...
In C, is "&" a operator or punctuator? (According to the info found online, I am still confused)
Name the project pa3 [Method 1] In the Main class, write a static void method to...
Name the project pa3 [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. [Method 2]...
3. [Method 1] In the Main class, write a static void method to print the following...
3. [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. 4. [Method 2] In the...
I am in a class where I am supposed to be Minister of Foreign Affair representing...
I am in a class where I am supposed to be Minister of Foreign Affair representing India. I was invited to the G20 meeting to discuss and present negotiations with other countries of G20. The agenda will have two major items: international health cooperation and international economic cooperation. What negotiations could I do for India with other countries to improve the current situation and look for financial stability?
I am in a class where I am supposed to be a Civil Society Organization Representative...
I am in a class where I am supposed to be a Civil Society Organization Representative (CSO) representing India. I was invited to the G20 meeting to discuss and present negotiations with other countries of G20.The agenda will have two major items: international health cooperation and international economic cooperation. What negotiations could I do for India with other countries to improve the current situation and look for financial stability?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT