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 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:

Homework Answers

Answer #1

When you will execute this code, your output will be like this,

Executing Line no. 2nd of above code, output of info method will be : I am an animal

Executing Line no. 4th of above code, output of info method will be : I am a reptile

Executing Line no. 6th of above code, output of info method will be : I am a bird

Explanation:

In First line of code you are creating a object animal of type Animal.

In 2nd line you are calling info method of animal which will call info method of Animal class as we have created and initialized animal with type of Animal. Quite simple.

Now, in 3rd line we are creating object of Reptile and assigning that object to animal which we have already created.

So, at 4th line by calling animal.info(), it will refer to the method ifo which is of class Reptile that's why it will print "I am a reptile" .

Same thing happens in case of Bird.

So Basically, this mechanism is nothing but Overriding which is very important aspect in inheritance. Overriding means that if we have one class named A which have method print(). And if we create one class named B which inherits class A means, Now class B have also print() method. till now nothing complecated it means if you calling print() method with class B's object, it will give same output as class A's print() method. But if you are providing some different defination of print() method in class B which is child class of class A then things might change. This is the case of Overriding. so, Overriding means that you have same name method in both parent and child class and providing different diffrent defination in both class for that same method. and now if we try to call print() methos using class B's object it will give output as per defination of print() method in class B. it means that method of child class is overriding the method of Parent class. in Overriding you can not call method of parent class using object reference of child class. it always referes child class's method.

Still you want to acess parent class method, then you have to create object of parent class in child class and call method of parent class in method of child class. For exmaple, if you want to call print() of class A then you have to create object of class A inside class B and you have to call print() of class A inside print() of class B using that object. but that means that whenever you call print() of class B it will call print() of class A as we are calling print() of class A from print() of class B.

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
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...
(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;    ...
Hi, I am trying to create an XML JTree viewer using the DOM parser instead of...
Hi, I am trying to create an XML JTree viewer using the DOM parser instead of the SAX parser, I am having trouble changing my code in order to utilize the DOM parser to extract the XML data into the tree structure. Would you be able to explain what changes should be made to the code in order to use the DOM parser instead of the SAX parser? // Java Packages //      import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import...
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the...
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the code to be written with these) I need Class river, Class CTRiver and Class Driver with comments so I can learn and better understand the code I also need a UML Diagram HELP Please! Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong()...
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...
i am trying to wrire a word and change it to capital letters using client-server. im...
i am trying to wrire a word and change it to capital letters using client-server. im not able to write any words when i run the file in netbeans or command promot. is it something with my code? /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package assignment3retake; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter;...
IntNode class I am providing the IntNode class you are required to use. Place this class...
IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined inline (within the class declaration). Do not write any other functions for the IntNode class. Use as is. struct IntNode { int data; IntNode *next;...