Overloading Examples

1. What is the output ?

public class A {

void add(int i) {
System.out.println("hi");
}

void add(byte i) {
System.out.println("hello");
}

public static void main(String[] args) {
A a = new A();
a.add(1);
}
}

2. What is the output ?

public class A {

void add(float i) {
System.out.println("hi");
}

void add(byte i) {
System.out.println("hello");
}

public static void main(String[] args) {
A a = new A();
a.add(1);
}
}

3. What is the output ?

public class A {

void add(Double i) {
System.out.println("hi");
}

void add(byte i) {
System.out.println("hello");
}

public static void main(String[] args) {
A a = new A();
a.add(1);
}
}

4. What is the output ?

public class A {

void add(Float i) {
System.out.println("hi");
}

void add(byte i) {
System.out.println("hello");
}

public static void main(String[] args) {
A a = new A();
a.add(1.0);
}
}

5. What is the output ?

public class A {

void add(Double i) {
System.out.println("hi");
}

void add(byte i) {
System.out.println("hello");
}

public static void main(String[] args) {
A a = new A();
a.add(1.0f);
}
}

6. What is the output ?

public class A {

void add(int[] i) {
System.out.println("hi");
}

void add(int ...i) {
System.out.println("hello");
}

public static void main(String[] args) {
A a = new A();
a.add(1);
}
}

7. What is the output ?

public class A {

void add(int i) {
System.out.println("hi");
}

void add(int ...i) {
System.out.println("hello");
}

public static void main(String[] args) {
A a = new A();
a.add(1);
}
}

8. What is the output ?

public class A {

void add(int i) {
System.out.println("hi");
}

void add(int ...i) {
System.out.println("hello");
}

public static void main(String[] args) {
A a = new A();
a.add();
}
}

9. What is the output ?

public class A {

void add(int i) {
System.out.println("hi");
}

void add(int ...i) {
System.out.println("hello");
}

public static void main(String[] args) {
A a = new A();
a.add(1,2);
}
}

10. What is the output ?

public class A {

void add(int i,float j) {
System.out.println("hi");
}

void add(int ...i,float j) {
System.out.println("hello");
}

public static void main(String[] args) {
A a = new A();
a.add(1,2);
}
}

11. What is the output ?

public class A {

void add(int i,float j) {
System.out.println("hi");
}

void add(float j,int ...i) {
System.out.println("hello");
}

public static void main(String[] args) {
A a = new A();
a.add(1,2);
}
}

12. What is the output ?

public class A {

void add(int i,float j) {
System.out.println("hi");
}

void add(float j,int i) {
System.out.println("hello");
}

public static void main(String[] args) {
A a = new A();
a.add(1,2);
}
}

13. What is the output ?

public class A {

void add(int i,float j) {
System.out.println("hi");
}

void add(float j,int i) {
System.out.println("hello");
}

public static void main(String[] args) {
A a = new A();
a.add(1,2.6);
}
}

14. What is the output ?

public class A {

void add(int i,float j) {
System.out.println("hi");
}

void add(float j,int i) {
System.out.println("hello");
}

public static void main(String[] args) {
A a = new A();
a.add(1,2.6f);
}
}

15. What is the output ?

public class A {

public static void main(String[] args) {
System.out.println(null);
}
}

16. What is the output ?

class Animal {}

class Dog extends Animal {}

public class A {
void show(Dog d) {
System.out.println("hi");
}

void show(Animal d) {
System.out.println("Hello");
}

public static void main(String[] args) {
Animal a=new Dog();
new A().show(a);
}
}

17. What is the output ?

class Animal {}

class Dog extends Animal {}

public class A {
void show(Dog d) {
System.out.println("hi");
}

void show(Animal d) {
System.out.println("Hello");
}

public static void main(String[] args) {
Dog a=new Dog();
new A().show(a);
}
}

18. What is the output ?

class Animal {}

class Dog extends Animal {}

public class A {
void show(Dog d) {
System.out.println("hi");
}

void show(Animal d) {
System.out.println("Hello");
}

public static void main(String[] args) {

new A().show(null);
}
}

19. What is the output ?

class Animal {}

class Dog extends Animal {}

public class A {
void show(Dog d) {
System.out.println("hi");
}

void show(Animal d) {
System.out.println("Hello");
}

public static void main(String[] args) {
Dog d=null;
new A().show(d);
}
}

20. What is the output ?


class Animal {}

class Dog extends Animal {}

public class A {
void show(Dog d) {
System.out.println("hi");
}

void show(Animal d) {
System.out.println("Hello");
}

public static void main(String[] args) {
Animal d=null;
new A().show(d);
}
}

21. What is the output ?

class Animal {}

class Dog extends Animal {}
class Donkey extends Animal {}

public class A {
void show(Dog d) {
System.out.println("hi");
}
void show(Donkey d) {
System.out.println("hi d");
}
void show(Animal d) {
System.out.println("Hello");
}

public static void main(String[] args) {

new A().show(null);
}
}

22. What is the output ?

class Person{}
class Student extends Person{}

class Animal {}
class Dog extends Animal {}

public class A {
void show(Person d) {
System.out.println("hi");
}

void show(Student d) {
System.out.println("hi student");
}

void show(Animal d) {
System.out.println("Hello");
}

public static void main(String[] args) {

new A().show(null);
}
}

23. What is the output ?

public class A {
int show(int d) {
return d;
}

double show(double d) {
return d;
}

public static void main(String[] args) {
System.out.println(new A().show(12.));
}
}

No comments: