Showing posts with label overloading. Show all posts
Showing posts with label overloading. Show all posts

Friday, 18 February 2011

Method overloading,overriding and static

public class Test {
public static void test() {
print();
}
public static void print() {
System.out.println("Test");
}
public void print() {
System.out.println("Another Test");
}
}
So this code will not work stating - duplicate method error.

Thursday, 17 February 2011

Overloading vs Overriding

In case of overloading in java, it doesn't matter what is the return type of the method. What matters is the name of the method should be same, and their signatures should be different. But in case of overriding even there return types must match.

Which of the following is a legal return type of a method overloading the following
method:

public void add(int a) {…}


A. void
B. int
C. Can be anything
Select the most appropriate answer.(Answer you know is C)

Which of the following is a legal return type of a method overloading the following
method:
public void add(int a) {…}
A. the overriding method must return void
B. the overriding method must return int
C. the overriding method can return whatever it likes
Select the most appropriate answer.(answer you know is A)