Pages

Saturday, January 10, 2015

Some java questions

1.

/*
 * prints output as
 *
 * Double array
 *
 */
public class NullCast {
public static void main(String args[]){
new NullCast().matchNull(null);
}
public void matchNull(Object obj){
System.out.println("Object");
}
public void matchNull(double[] obj){
System.out.println("Double array");
}
}


2.

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
/*
 * prints output as below
 *
 * list
 *
 */

public class MethodParameter {
public static void main(String args[]){
List<String> list = Arrays.asList(new String []{""});

new MethodParameter().method(list);

//error if list was declared as "List list = " i.e. (without generic type)
//method(Collection<Object>) is ambiguous for the type MethodParameter
}

public void method(Collection<Object> obj){
System.out.println("collection");
}
public void method(List<String> str){
System.out.println("list");
}
}

3.

/*
 * prints output as below
 *
 * woof woof
 *
 */
public class StaticMethods {
public static void main(String args[]){
Dog dog = new Dog();
Dog basenji = new Basenji();
dog.bark();
basenji.bark();
}
}
class Dog{
public static void bark(){
System.out.print(" woof ");
}
}
class Basenji extends Dog{
public static void bark(){

}
}

No comments:

Post a Comment