Java Interview Question Set 12
Categories: Java 8(JDK1.8)
What is object cloning?
The object cloning is used to create the exact copy of an object. The clone() method of the Object class is used to clone an object. The java.lang.Cloneable interface must be implemented by the class whose object clone we want to create. If we don't implement Cloneable interface, clone() method generates CloneNotSupportedException.
protected Object clone() throws CloneNotSupportedException
What is method overloading?
Method overloading is the polymorphism technique which allows us to create multiple methods with the same name but different signature. We can achieve method overloading in two ways.
a) By Changing the number of arguments
b) By Changing the data type of arguments
Method overloading increases the readability of the program. Method overloading is performed to figure out the program quickly.
Why is method overloading not possible by changing the return type in java?
In Java, method overloading is not possible by changing the return type of the program due to avoid the ambiguity.
class Adder{
static int add(int a,int b){return a+b;}
static double add(int a,int b){return a+b;}
}
class TestOverloading3{
public static void main(String[] args){
System.out.println(Adder.add(11,11));//ambiguity
}}
Output:
Compile Time Error: method add(int, int) is already defined in class Adder
Can we overload the methods by making them static?
No, We cannot overload the methods by just applying the static keyword to them(number of parameters and types are the same). Consider the following example.
public class Animal
{
void consume(int a)
{
System.out.println(a+" consumed!!");
}
static void consume(int a)
{
System.out.println("consumed static "+a);
}
public static void main (String args[])
{
Animal a = new Animal();
a.consume(10);
Animal.consume(20);
}
}
Output
Animal.java:7: error: method consume(int) is already defined in class Animal
static void consume(int a)
What is method overloading with type promotion?
By Type promotion is method overloading, we mean that one data type can be promoted to another implicitly if no exact matching is found.
As displayed in the above diagram, the byte can be promoted to short, int, long, float or double. The short datatype can be promoted to int, long, float or double. The char datatype can be promoted to int, long, float or double and so on. Consider the following example.
What is the output of the following Java program?
class OverloadingCalculation3{
void sum(int a,long b){System.out.println("a method invoked");}
void sum(long a,int b){System.out.println("b method invoked");}
public static void main(String args[]){
OverloadingCalculation3 obj=new OverloadingCalculation3();
obj.sum(20,20);//now ambiguity
}
}
Output
OverloadingCalculation3.java:7: error: reference to sum is ambiguous
obj.sum(20,20);//now ambiguity
^
both method sum(int,long) in OverloadingCalculation3
and method sum(long,int) in OverloadingCalculation3 match
1 error
What is method overriding:
If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding. It is used for runtime polymorphism and to implement the interface methods.
Rules for Method overriding
a) The method must have the same name as in the parent class.
b) The method must have the same signature as in the parent class.
c) Two classes must have an IS-A relationship between them.
Why can we not override static method?
It is because the static method is the part of the class, and it is bound with class whereas instance method is bound with the object, and static gets memory in class area, and instance gets memory in a heap.
Difference between method Overloading and Overriding.
Method OverloadingMethod Overriding
1) Method overloading increases the readability of the program.Method overriding provides the specific implementation of the method that is already provided by its superclass.
2) Method overloading occurs within the class.Method overriding occurs in two classes that have IS-A relationship between them.
3) In this case, the parameters must be different.In this case, the parameters must be the same.
Can we override the private methods?
No, we cannot override the private methods because the scope of private methods is limited to the class and we cannot access them outside of the class.