Question

Given the method definiton public void foo(List<Object>) { ... } What happens if you want to...

Given the method definiton

public void foo(List<Object>) { ... }

What happens if you want to call

foo(new ArrayList<String>);

Homework Answers

Answer #1

Based on your question above I deduce your foo method to be as follows

public void foo(List<Object> ob)

{

//Lines of code;

}

Now based on the method call you have shown there can be two cases I can deduce

1. foo(new ArrayList<String>);--- Without bracket after String

&

2.foo(new ArrayList<String>());-- With Bracket after String

I will tell you what will happen in both the cases, for the first case it will give the below error

error: '(' or '[' expected as you are missing the parenthesis after the constructor.

For the second case if there is the() bracket . We will still get an error

error: incompatible types: ArrayList cannot be converted to List

even though String is a sub-class of Object as in Generic type polymorphism is not allowed it is only applicable to collection types.

if it were foo(new ArrayList<Object>()); or foo(new ArrayList<? extends Object>()); both would have been valid and the program would have executed successfully.

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
THIS IS A JAVA PROGRAM THAT NEEDS TO BE WRITTEN Write a recursive method void reverse(ArrayList<Object>...
THIS IS A JAVA PROGRAM THAT NEEDS TO BE WRITTEN Write a recursive method void reverse(ArrayList<Object> obj) that reverses an ArrayList of any type of object. For example, if an ArrayList held 4 strings: "hi", "hello", "howdy", and "greetings" the order would become "greetings", "howdy", "hello", and "hi". Implement a recursive solution by removing the first object, reversing the ArrayList consisting of the remaining Objects, and combining the two. Use the following class ArrayListReverser to write and test your program....
Give the output of the following program. Then explain what the foo() method does, in general,...
Give the output of the following program. Then explain what the foo() method does, in general, for a given input. Is foo() a tail-recursive method? Why or why not? class SomeThing {     public static void main(String args[]) {         System.out.println(foo(6));     }     private static int foo(int input) {         if (input <= 0) {             throw new RuntimeException("invalid input");         }         else if (input == 1) {             return 1;         }         else if (input %...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will be filled with Car objects. It will call the displayMenu method to display the menu. Use a sentinel-controlled loop to read the user’s choice from stdin, call the appropriate method based on their choice, and redisplay the menu by calling displayMenu. Be sure to use the most appropriate statement for this type of repetition. displayMenu Parameters:             none Return value:          none Be sure to use...
I need a method for public void ****************** write the method “insertDoubles” that for each value...
I need a method for public void ****************** write the method “insertDoubles” that for each value found in an integer linked list it inserts that value doubled after the original. The method should return nothing and take in in no parameters. If the list is empty, then the method should do nothing. To avoid infinite loops you need to move the iterator past the newly inserted value. here is the provided code public class Question02 { public class ListNode//public for...
Question 6 (6 pts) The interface SayHello is defined as followed: public interface SayHello {             void...
Question 6 (6 pts) The interface SayHello is defined as followed: public interface SayHello {             void printGreeting( ); } Write the statements to instantiate an object of the anonymous class that implements the above interface by using the following definition of printGreeting(   ) and then call the instance method printGreeting( ). void  printGreeting(    ) {             System.out.println(“Hello guys”); } Question 7 (8 pts) The abstract class SayHello and the static method process are defined as followed: class SayHello {             private String greeting; SayHello(  ...
1.If you have defined a class,  SavingsAccount, with a public  static method,  getNumberOfAccounts, and created a  SavingsAccount object referenced by...
1.If you have defined a class,  SavingsAccount, with a public  static method,  getNumberOfAccounts, and created a  SavingsAccount object referenced by the variable  account20, which of the following will call the  getNumberOfAccounts method? a. account20.getNumberOfAccounts(); b. SavingsAccount.account20.getNumberOfAccounts(); c. SavingsAccount.getNumberOfAccounts(); d. getNumberOfAccounts(); e. a & c f. a & b 2.In the following class, which variables can the method printStats use? (Mark all that apply.) public class Item { public static int amount = 0; private int quantity; private String name; public Item(String inName, int inQty) { name...
1-To create an ArrayList of strings, which of the following is correct? `a ArrayList<String> list =...
1-To create an ArrayList of strings, which of the following is correct? `a ArrayList<String> list = new ArrayList<String>(); b ArrayList<String> list = new String<String>​[]; c ArrayList<String> list = new ArrayList<String>; d ArrayList​[String] list = new ArrayList​[String]; 2-All instance variables? a Are assigned a default value if they are not initialized b Must be initialized as part of their declaration c Are assigned an arbitrary value if they are not initialized d Must be initialized by the constructor 3-An instance method...
C++ See the provided specification files. Complete the implementation for each as a separate file. void...
C++ See the provided specification files. Complete the implementation for each as a separate file. void seen(std::string); If there is already a Word object in the Words list, then the number of occurrences for this word is incremented. If there is no Word object for this word already, create a new word object with occurrence =1, and insert this object into the list of Word objects. std::string getNextWord(); Returns the next word of the list and sets the currentItem pointer...
java Considering the following code segment, answer the multiple choice questions. public String foo(String input) {...
java Considering the following code segment, answer the multiple choice questions. public String foo(String input) { String str = input.toLowerCase(); return bar(str); } private String bar(String s) { if (s.length() <= 1)   {return "";} else if (s.length() % 2 == 0)   {return "*" + bar(s.substring(2, s.length()));} else if (s.length() % 2 == 1) {return "*" + bar(s+"n");} else {return "";} } Which line(s) indicate the name of the helper method? Which line(s) contains recursive call(s)? How many * are in...
Question 1: Write the following method that returns a new ArrayList. The new list contains the...
Question 1: Write the following method that returns a new ArrayList. The new list contains the nonduplicate elements from the original list, public static <E> ArrayList<E> removeDuplicates(ArrayList<E> list) Question 2: Implement the following method that returns the maximum element in an array: public static <E extends Comparable<E>> E max(E[] list) Each question is its own code and it must be able to run. Please show details . Thank you in advance!
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT