For each of the numbered lines of code below, state whether it will compile. If it does, state and explain the output it gives w | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

For each of the numbered lines of code below, state whether it will compile. If it does, state and explain the output it gives w

public static class A { public void print() {System.out.println("A");} public void printObject(A a) {System.out.println("A");} public static void printAll(List<A> list) { for (A a : list) a.print(); } } public static class B extends A { public void print() {System.out.println("B");} public void printObject(B b) {System.out.println("B");} public static void main(String[] args) { A a = new A(); B b = new B(); LinkedList<A> alist = new LinkedList<A>(); alist.add(a); alist.add(b); LinkedList<B> blist = new LinkedList<B>(); blist.add(b); 1 ((B)a).print(); 2 ((A)b).print(); 3 A.printAll(alist); 4 b.printAll(blist); 5 a.printObject(b); 6 b.printObject((B)a); 7 b.printObject((A)b); } }//Q For each of the numbered lines of code below, state whether it will compile. If it does, state and explain the output it gives when run. If it does not, explain why not.

1st Dec 2016, 10:18 AM
Goldwyn Zimba II
Goldwyn Zimba II - avatar
1 Answer
+ 12
It is a wall of code and it looks like homework. Could you solve it on your own? Or do you still need help? If so, try to publish the code example at the code playground and post a new question. You can add a link to the new question.
24th Apr 2017, 9:53 PM
Tashi N
Tashi N - avatar