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
What does the following application print and explain why it happens? public class Foo {            ...
What does the following application print and explain why it happens? public class Foo {             public void crazyArray(int[] vals){                   int[] newVals = { 1, 7, 6, 1, 3 };                   vals[0] -= vals[0] - newVals[3];                   vals[3] = vals[2] + vals[1] - newVals[4];                   vals[2] -= vals[0] *2;                   vals = newVals;                   vals[1] -= vals[2];                   newVals[2] *= 3 + vals[3];                   vals[1] -= 14 + vals[2];             }                         public static void main(String[] args) {...
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...
Add a method to CatList with this header public ArrayList getCatsSortedByName() which sorts the inner ArrayList...
Add a method to CatList with this header public ArrayList getCatsSortedByName() which sorts the inner ArrayList of cats and returns it. Use an anonymous inner class which implements Comparator. public static void main(String[] args) { CatList cats = new CatList(); System.out.println("will create the cats"); cats.addCat(new Cat("Frank", "gray")); cats.addCat(new Cat("Zen", "black")); cats.addCat(new Cat("Xiongmao", "spotted")); cats.addCat(new Cat("Little Kit", "calico")); cats.addCat(new Cat("Ang", "gray")); System.out.println("Here is the list"); System.out.println(cats); System.out.println("There are " + cats.howManyCats() + "cats on the list");       //ArrayList<Cat> sortedCats =...
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(  ...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any. What is the big-O complexity of your method in terms of the list size n. Supplementary Exercise for Programming (Coding) [Stacks] Download and unpack (unzip) the file Stacks.rar....
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT