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

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 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.
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?
I have a urinary analysis that I am working on patient info: 12 yrs old, losing...
I have a urinary analysis that I am working on patient info: 12 yrs old, losing weight, always hungry, getting up twice to use the bathroom Physical exam: respirations: 15/min BP: 125/80mmHg Pulse: 75/min temp:37C BLood values: pH:735 WBC: 8200/mm blood glucose: 260 mg/dL albumin: 4.0g/L Urinalysis pH: 5.0 glucose: 1,00mg/dL ketone: large specifc gravity: 1.006 leukocytes, nitrite, protein: negative color: light yellow, clear Question: why is this patient always hungry if she is eating well? explain anatomy and physiology
I am taking, environmental geology, business law, algebra I, and info technology concepts and business software....
I am taking, environmental geology, business law, algebra I, and info technology concepts and business software. Do you cover any of these topics?
I am working on extensive report for my Advanced Corp. Tax class and I am hoping...
I am working on extensive report for my Advanced Corp. Tax class and I am hoping to get a better understanding of the following: -In order for a corporation to make a valid “S” election, the corporation must be a “small business corporation.” Explain the meaning of “small business corporation” in this context. -Explain what ‘separately stated items’ are in regards to Partnerships and S corporations? Give an example of ‘separately stated items’ for each type of entity. -What requirements...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT