33. What is Method overloading in Java?
Ans: When a class has more than one method having the same name but with different parameter lists, this feature is called method overloading in Java.
34. How Java compiler differentiate overloaded methods?
Ans: Java compiler differentiates overloaded methods by their signatures. The signature of a method is defined by its name and list of parameters.
The return type of a method is not part of the method signature. It does not play any role in resolving methods overloaded.
35. What is another name of method overloading?
Ans: Method overloading is also known as static (compile-time) polymorphism.
36. How is method overloading implemented in Java?
Ans: To implement method overloading in Java, we will have to define two methods with same name in a class and do one/more of the following:
- Different number of parameters
- Different data type of parameters
- Different sequence of data type of parameters
37. What are the rules of method overloading to implement in Java?
Ans: There are certain rules by which method overloading can be implemented in Java. They are as follows:
1. The method name must be the same.
2. Parameters must be different i.e. each overloaded method must take a unique list of parameter types. The parameters can be changed in one of the following three ways:
- Number of parameters
- Data type of parameters
- Sequence of data type of parameters
3. Access modifiers can be anything or different.
4. Return type can be anything or different.
5. Exception thrown can be anything.
38. When to use Method overloading in Java?
Ans: Method overloading should be used when we are doing the same tasks with different parameters in multiple methods.
If multiple methods perform different tasks, don’t use the method overloading concept. Method overloading is done in java due to the following needs:
- Method overloading is done to reuse the same method name.
- It is done to make the program logically more readable and understandable.
- It is used to achieve the compile-time polymorphism in Java.
39. Why is not possible to implement method overloading by changing return type of method in java?
Ans: Method overloading cannot be done (implemented) by changing the return type of the method because there may occur ambiguity. But the overloaded methods can change the return type.
40. Is it possible to overload main() method in Java?
Ans: Yes, Java allows programmers to define multiple methods with same name ‘main’. But execution will always be started public static void main(String[] args) method.
41. Why Java does not support operator overloading?
Ans: Java supports Method overloading but does not support operator overloading because it would make the design more complex by incorporating operator loading.
Operator overloading will also reduce the performance of JVM, since JVM has to do extra work to find the real meaning of overloaded operators at run time.
42. Can private and final methods be overloaded?
Ans: Yes, private and final methods can be overloaded.
43. What is the error in the following code?
public class Test {
public static void m1(int x) {
}
public static int m1(int y){
return y;
}
public static void main(String[] answer) { }
}
Ans: Duplicate method error because overloading is not possible by changing return type.
44. What will be output of the following code?
public class Test {
private int m1(int x){
return 20;
}
private String m1(String x){
return "abc";
}
public static void main(String[] answer) {
Test t = new Test();
System.out.println(t.m1(20));
System.out.println(t.m1(null));
}
}
Ans: Output is 20, abc.
45. Will the code compile successfully? If yes, what will be the output of the code?
public class A {
void m1(Object o){
System.out.println("Hello");
}
void m1(Object o, String s) {
System.out.println("Java");
}
public static void main(String[] args)
{
A a = new A();
a.m1(new Object());
a.m1(new Object(), new String());
}
}
Ans: Yes, the code will be compiled successfully. The output is Hello, Java.
46. What will be the output of the following program after execution?
public class A {
void m1(Integer i){
System.out.println("Hello");
}
void m1(Object o){
System.out.println("Java");
}
public static void main(String[] args){
A a = new A();
a.m1(null);
}
}
Ans: Output: Hello.
47. Will the following code compile successfully? If yes, what will be the output after execution?
public class A {
void m1(Integer i) {
System.out.println("Hello");
}
void m1(String s) {
System.out.println("Java");
}
public static void main(String[] args) {
A a = new A();
a.m1(null);
}
}
Ans: Compile time error: The method m1(Integer) is ambiguous for the type A
48. Will the program compile successfully? If yes, what will be output after execution?
public class A {
void m1(Integer i) {
System.out.println("Hello");
}
void m1(Double d) {
System.out.println("Java");
}
public static void main(String[] args)
{
A a = new A();
a.m1((Integer) new Object());
}
}
Ans: Yes, the code will be compiled successfully. But ClassCastException will be thrown at runtime because Object cannot be casted to Integer.
49. Show the output of the following program.
public class A {
void m1(int i)
{
System.out.println("Hello");
}
void m1(Double d)
{
System.out.println("Java");
}
public static void main(String[] args)
{
A a = new A();
a.m1(new Integer(25));
}
}
Ans: Output: Hello.
For more coding programming questions based on method overloading, go to this tutorial: Method overloading interview programming questions
50. What is Type conversion in Java?
Ans: The process of converting a value from one data type to another is known as type conversion in Java. It is also known as type casting in java or simply ‘casting’.
51. What is Implicit type casting in Java?
Ans: Automatic conversion (casting) done by Java compiler internally is called implicit conversion or implicit type casting in java. It is performed to convert a lower data type into a higher data type. It is also known as automatic type promotion in Java.
52. When takes place Automatic type conversion in Java?
Ans: An automatic type conversion takes place in Java if these two conditions are met:
- The data types are compatible with each other.
- The destination type is bigger than the source type.
53. What is widening in Java?
Ans: The process of converting lower data type into higher data type is called widening in java.
54. Is it possible widening conversion from numeric type to char or boolean?
Ans: No, because the numeric type is incompatible with char or boolean.
55. Identify the error in the following code.
public class A {
public static void main(String[] args) {
int a = 50;
long l = a;
int i = l;
System.out.println(+l);
}
}
Ans: Error in line no 5 because we cannot convert from long to int.
56. What is narrowing conversion in Java?
Ans: The conversion of a higher data type into a lower data type is called narrowing conversion. Since it is performed by the programmer, therefore, it is also called explicit type casting in java.
57. What is the difference between implicit casting and explicit casting?
Ans: Refer to this tutorial: Type conversion and Casting in Java.
58. What is Upcasting in Java?
Ans: When the reference variable of super class refers to the object of subclass, it is known as upcasting in java.
59. What is the output of the following program if no error?
public class One {
void m1() {
System.out.println("m1 method in class One");
}
}
public class Two extends One {
void m1(){
System.out.println("m1 method in class Two");
}
}
public class Test {
public static void main(String[] args) {
One o = (One)new Two();
o.m1();
}
}Ans: Output: m1 method in class Two.
60. What is Downcasting in Java?
Ans: When subclass reference refers to super class object, it is called downcasting in java.
61. What is the output of the following code?
public class One {
void m1(){
System.out.println("m1 method in class One");
}
}
public class Two extends One {
void m2() {
System.out.println("m2 method in class Two");
}
}
public class Test {
public static void main(String[] args) {
Two t = (Two) new One();
t.m1();
t.m2();
}
}Ans: Exception in thread “main” java.lang.ClassCastException: classCasting.One cannot be cast to classCasting.Two
62. Will the code compile successfully? If yes, what is the output of application?
public class One {
void m1() {
System.out.println("m1 method in class One");
}
}
public class Two extends One{
void m2() {
System.out.println("m2 method in class Two");
}
}
public class Test {
public static void main(String[] args) {
One o = new Two();
Two t = (Two)o;
t.m1();
t.m2();
}
}Ans: The code will be compiled successfully. Output: m1 method in class One, m2 method in class Two.
For explanation of this code, go to this tutorial: Upcasting and Downcasting in Java
63. What is Method overriding in Java?
Ans: When the method of superclass is overridden in the subclass to provide more specific implementation, it is called method overriding.
64. What are Overridden and Overriding methods in Java?
Ans: The superclass method which is overridden is called overridden method.
The subclass method which is overriding the superclass method is called overriding method.
65. Why do we need to create Subclass in Java?
Ans: There are three reasons to create subclass of superclass in Java. They are as follows:
- To add a new feature or properties.
- To override or change the existing functionality of superclass method.
- To inherit the existing functionality of superclass method.
66. What is the use of method overriding in Java?
Ans: Method overriding is used to achieve runtime polymorphism in Java. It is used to change the existing functionality of the superclass method.
67. How to implement method overriding in Java?
Ans: To override a method, we need to provide a new implementation of a method with the same signature in subclass. There will be at least two implementations of the method with the same signature. One implementation is in superclass and another implementation is in subclass.
68. What are the important rules of method overriding in Java?
Ans: Refer to this tutorial: Method overriding in Java
69. Can we reduce the visibility of overriding method in subclass?
Ans: While the overriding method, we can increase the visibility of the overriding method but cannot reduce it.
70. Will the code compile successfully? If yes, what is the output after execution?
public class X {
public void m1(){
System.out.println("m1-X");
}
}
public class Y extends X {
void m1(){
System.out.println("m1-Y");
}
}
public class XY {
public static void main(String[] args) {
Y y = new Y();
y.m1();
}
}Ans: No, the code will not compile successfully because we cannot reduce the visibility of overriding method in subclass.
71. Why cannot private method be overridden?
Ans: We cannot override private method because when superclass method is private, it will not be visible to the subclass.
72. Can we override static method in Java?
Ans: No, we cannot override a static method. We cannot also override a static method with an instance method because static method is bound with class whereas an instance method is bound with an object.
73. Can we stop method overriding in Java?
Ans: Yes, we can stop method overriding by declaring method as final.
74. What is Covariant return type in Java?
Ans: If the return type of overriding method in the subclass is a subtype of the declared return type of overridden method instead of being exactly the same type, it is known as covariant return types in Java.
75. What are the rules of covariant return type in Java?
Ans: There are mainly three rules for covariant return types. They are:
- The return type of overriding method in the subclass should be either the same as the return type of superclass or subclass.
- The return type of overriding method in the subclass should not be a parent of the parent method return type.
- The covariant return type is applicable only for object types not for primitive types.
76. What will be the output of the following code?
public class X {
public Object m1(char c)
{
System.out.println("m1-X");
return new X();
}
}
public class Y extends X {
public StringBuffer m1(char c)
{
System.out.println("m1-Y");
return null;
}
}
public class XY {
public static void main(String[] args) {
X x = new Y();
x.m1('a');
X x1 = new X();
x1.m1('b');
}
}Ans: Output: m1-Y, m1-X.
For more coding questions with explanation, go to this tutorial: Covariant Return type in Java
77. Identify the error in the following code?
public class Parent {
void msg(){
System.out.println("msg-Parent");
}
}
import java.io.IOException;
public class Child extends Parent
{
void msg() throws IOException {
System.out.println("msg-Child");
}
public static void main(String[] args) throws IOException {
Parent p = new Child();
p.msg();
Child c = new Child();
c.msg();
}
}Ans: Unresolved compilation problem: Exception IOException is not compatible with throws clause in Parent.msg(). This is because the overriding method is throwing a checked exception.
78. Is there any error in the following code? If not, what will be the output of the program?
public class Parent {
void msg() {
System.out.println(“msg-Parent”);
}
}
import java.io.IOException;
public class Child extends Parent
{
void msg() throws ArithmeticException {
System.out.println(“msg-Child”);
}
public static void main(String[] args) throws IOException {
Parent p = new Child();
p.msg();
Child c = new Child();
c.msg();
}
}Ans: No error because the overriding method is throwing an unchecked exception. Output: msg-Child, msg-Child.
79. Identify the errors in the following code.
a)
In superclass
public void m1() throws IOException {
System.out.println("Hello");
}
In subclass
public void m1() throws Exception {
System.out.println("Hi");
}Ans: Compile-time error because the overriding method is throwing an exception which is the superclass of the superclass method’ exception. i.e. Exception is the superclass of IOExecption.
b)
In superclass
public void m1() throws Throwable {
System.out.println("Parent");
}
In subclass
public void m1() throws Exception {
System.out.println("Child");
}Ans: No compile-time error because Throwable is the superclass of all exceptions.
c)
In base class
public void m1() throws Exception {
System.out.println("Base");
}
In derived class
void m1() throws InterrurptedException {
System.out.println("Child");
}Ans: Compile-time error because we cannot reduce the visibility of inherited method from Parent. Access modifiers must be bigger or same.
d)
In superclass
protected void m1(char c) throws Throwable {
System.out.println("Parent");
}
In subclass
void m1(char c) {
System.out.println("Child");
}Ans: Error because we cannot reduce the visibility while overriding the method.
For explanation and rules, refer to this tutorial: Exception handling with Method overriding.



