Question

In: Computer Science

write exactly what the following code prints. Ensure that you write in the correct order. clearly...

write exactly what the following code prints. Ensure that you write in the correct order. clearly distinguish scratch work from your final answer.

Public class Base{

public void m1(Base a){

System.out.println("Base m1");

a.m2();

}

public void m2(){

System.out.println("Base m2");

}

}

Public class Derived1 extends Base{

public void m1(Base b){

System.out.println("Derived1 m1");

}

public void m2(){

System.out.println("Derived1 m2");

}

}

Public class Derived2 extends Base{

public void m2(){

System.out.m2(){

System.out.println("Dertived2 m2");

}

}

public class UserBaseAndDerived1And2{

public static void main(String[] args){

Base b = new Base();

Derived1 d1 = new Derived1();

Derived2 d2 = new Derived2();

b.m1(b);

b.m1(d1);

b.m1(d2);

b =d1;

b.m1(d2);

b = d2;

b.m1(d1);

}

}

Solutions

Expert Solution

Output:

Base m1
Base m2
Base m1
Derived1 m2
Base m1
Dertived2 m2
Derived1 m1
Base m1
Derived1 m2

// here calling m1() with object type Base so it will execute base class m1()
       // from there we are calling m2 which executes in m2()
       b.m1(b);
       // here calling m1() with object type Base so it will execute base class m1()
       // passing d1 object if type Dereived1 so m2() will called using d1 so it will execute m2() in the Derived1 class
       b.m1(d1);
       // here calling m1() with object type Base so it will execute base class m1()
       // passing d2 object if type Dereived2 so m2() will called using d1 so it will execute m2() in the Derived2 class
       b.m1(d2);
       //now b is pointing to Derived1 class object
       b = d1;
//calling m1() using b, now b is pointing Derived1 object so it will execute m1() in Derived1
// calling m2() using d2 so it will call in Derived2 class
       b.m1(d2);
// now b is pointing d2
       b = d2;
// now m1() in Derived2 will be executed
// passing d1 so m2() will be executed in Derived1
       b.m1(d1);


Related Solutions

What will be the expected output of the following pseudo code? Write exactly what would display...
What will be the expected output of the following pseudo code? Write exactly what would display when you execute the statements. Module main() Declare Integer a = 5 Declare Integer b = 2 Declare Integer c = 3 Declare Integer result = 0 Display "The value of result is" Display result Set result = a + b * c - a Display "Changed value is: ", result End Module
1. Write a shell code that prints the integers from 80-90 2. Write a shell code...
1. Write a shell code that prints the integers from 80-90 2. Write a shell code that prints the sum of odd integers that are inputted by the user 3. Write a shell code that reads a set of integers {125, 0, 122, 129, 0, 117} and prints {125, 122, 129, 117} Programming Language: Linux
Write the correct code in SQL 1. What is the name and address of the customer...
Write the correct code in SQL 1. What is the name and address of the customer that placed order 57? 2. Assume there is only one product with the description Cherry End Table. List the names of the raw materials that go into making that product. 3. List the product id, description, and finish of the least expensive products. (Note: A couple of rows show a price of 0. Exclude products with a price of 0 from your query.) 4....
Write the correct code in SQL 1. What is the name and address of the customer...
Write the correct code in SQL 1. What is the name and address of the customer that placed order 57? 2. Assume there is only one product with the description Cherry End Table. List the names of the raw materials that go into making that product. 3. List the product id, description, and finish of the least expensive products. (Note: A couple of rows show a price of 0. Exclude products with a price of 0 from your query.) 4....
Write code in Python that does the following : An anagram is when the order of...
Write code in Python that does the following : An anagram is when the order of a word can be changed to create a new word (evil, live, and vile for example). Using the dictionary provided, find all of the anagram groups. Which words contain the most anagrams? How large is this group? Here is another example: Alert, alter, and later have 3 in their group. Use the provided dict.txt as your word list, solution must also finish in less...
Write a python code which prints triangle of stars using a loop ( for loop )...
Write a python code which prints triangle of stars using a loop ( for loop ) Remember what 5 * "*" does The number of lines of output should be determined by the user. For example, if the user enters 3, your output should be: * ** *** If the user enters 6, the output should be: * ** *** **** ***** ****** You do NOT need to check for valid input in this program. You may assume the user...
with java code write The method prints a meaningful title with your name as the author....
with java code write The method prints a meaningful title with your name as the author. The method then generates THREE random numbers 0 - 4 and reports how many of the numbers are the same. 2-Use a loop to repeat 10 times. the output should be like this Title title title by Your Name 4, 3, 4 two are the same 4, 0, 0 two are the same . . . 2, 0, 4 none are the same 4,...
Write a Java application that accepts a bar code as a command line parameter and prints...
Write a Java application that accepts a bar code as a command line parameter and prints out the ZIP code. Assume that the bar code uses the symbols "|" and ":" for the long and short bars, respectively. Provide warnings on errors in the bar code specifying what exactly is wrong. The bar code input should be in the format specified in Problem 1, including the pair of the full bars at the beginning and at the end. Important: The...
Complete the following in syntactically correct Python code. Write a program, using a for loop, that...
Complete the following in syntactically correct Python code. Write a program, using a for loop, that calculates the amount of money a person would earn over a period of time if his or her salary is 1 penny for the first day, 2 pennies for the second day, 4 pennies for the third day, and continues to double each day. 1.      The program should ask the user for the number of days the employee worked. 2.      Display a table showing the salary...
Write code that takes the size of their foot from user and prints out suggested sandal...
Write code that takes the size of their foot from user and prints out suggested sandal size. Shoe Range Sandal Size <= 5 (inclusive) Small 5 ~ 9 (inclusive) Medium 9 ~ 12 (inclusive) Large above 12 X-Large example: Please enter your shoe size: (user types 6.5) Your sandal size is Medium.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT