Question

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 Mammal both extend class Animal, and both override method info(). Class Bird implements method info to print "I am a bird", and class Mammal implements method info to print "I am a mammal". Determine the outcome printed by the following lines of code.

Animal animal = new Mammal();

animal.info(); //OUTCOME:

animal = new Animal()

animal.info(); //OUTCOME:

animal = new Bird();

animal.info(); //OUTCOME:

Homework Answers

Answer #1

Java supports runtime polymorphism also known as dynamic polymorphism. Call to an overridden method is resolved at runtime rather than at compile-time. As per this, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable.

Animal animal = new Mammal();

animal.info(); //OUTCOME: "I am a mammal"

animal = new Animal()

animal.info(); //OUTCOME: "I am an animal"

animal = new Bird();

animal.info(); //OUTCOME: "I am a bird"

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
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...
Write a function with the prototype void stars(int i);. The function prints a line of i...
Write a function with the prototype void stars(int i);. The function prints a line of i stars followed by a new line. Write code to apply the stars function to every node in some binary tree called Tree using an in-order traversal, where the number of stars printed is given by the data of the node in the BST. You must write both the function definition and the code that uses it with the star function.
public class Programming { public static void main(String args[]) { System.out.println("I am learning how to program.");...
public class Programming { public static void main(String args[]) { System.out.println("I am learning how to program."); } } Compile this and execute this program then modify it to display I am learning how to program in Java. Change class name to Javaprogramming.java. compile and execute. Modify the Javaprogramming class so it prints two lines of output. Change name to Awesome. Add second output statement that displays That's awesome ! Save as awesome.java then compile and execute
What's wrong with this code? #Java Why I am getting error in : int BusCompare =...
What's wrong with this code? #Java Why I am getting error in : int BusCompare = o1.numberOfBusinesses.compareTo(o2.numberOfBusinesses); public class TypeComparator implements Comparator<Type> { @Override public int compare(Type o1, Type o2) { // return o1.name.compareTo(o2.name); int NameCompare = o1.name.compareTo(o2.name); int BusCompare = o1.numberOfBusinesses.compareTo(o2.numberOfBusinesses); // 2-level comparison using if-else block if (NameCompare == 0) { return ((BusCompare == 0) ? NameCompare : BusCompare); } else { return NameCompare; } } } public class Type implements Comparable<Type> { int id; String name; int...
Assume the method giveBonus() has been added to the BankAccount class. public class Raise { private...
Assume the method giveBonus() has been added to the BankAccount class. public class Raise { private int annualSalary; public Raise(){ annualSalary = 0; } //end constructor public Raise(int currentSalary){ annualSalary = currentSalary; } //end constructor public void giveRaise(){ annualSalary = annualSalary + 500; } //end giveRaise public void getSalary(){ return annualSalary; } //end giveRaise } What will be the output from the following statements that use this BankAccount class? (assume there is a getBalance() method that returns the balance) Raise...
I am having some trouble writing some java code involving strings. I have included the code...
I am having some trouble writing some java code involving strings. I have included the code provided by my professor below. that has the instructions in it as well public class StringExercise { public static void main(String[] args) { String given = "The quick brown fox jumped over the moon!"; String given2 = "mary"; String given3 = "D"; //Write a Java code snippet to check (print a boolean) whether the given String ends with the contents of "oon!", see endsWith...
Hello i am working on a project for my programming course and i am a little...
Hello i am working on a project for my programming course and i am a little lost. The assignment is as follows: Part A Do all of problem 3.13 (Employee Class), including the main, as written. Part B Now, suppose a new company policy says that instead of the monthly salary we will now store the yearly salary. We will need to make this change inside Employee and support the new usage, without breaking existing code (such as the old...
Question 3 a) Add a new class named CAS (Contract Academic Staff) to the project. It...
Question 3 a) Add a new class named CAS (Contract Academic Staff) to the project. It must inherit from Professor, but add a new String attribute named term. (The term must be a six digit string, with the year followed by two digits to represent the term: "01" for Winter, "05" for Spring, and "09" for Fall. Thus, 201705 represents the Spring 2017 term. Override print so that it prints CAS data like: McGarrity, Ivan Department: English Term: 201705 Write...
Write a program that utilizes a Professor class. A professor has a first name, last name,...
Write a program that utilizes a Professor class. A professor has a first name, last name, affiliation, easiness grade, and a helpfulness grade. Grades range from 1 (lowest) to 5 (highest). The Professor class has two constructors. The first constructor initiates the object with just the first name, the last name, and the affiliation. The default value for easiness and helpfulness grade is 3. The second constructor initiates the object using the first name, the last name, the affiliation, and...
(Please use Java Eclipse) QUESTION 1: For the question below, assume the following implementation of LinkedQueue:...
(Please use Java Eclipse) QUESTION 1: For the question below, assume the following implementation of LinkedQueue: public static final class LinkedQueue<T> implements QueueInterface<T> {     private Node<T> firstNode;     private Node<T> lastNode;     public LinkedQueue() {         firstNode = null;         lastNode = null;     }     @Override     public T getFront() {         if (isEmpty()) {             return null;         }         return firstNode.getData();     }     @Override     public boolean isEmpty() {         return firstNode == null;    ...