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

Friday, 23 December 2016

Interview Questions based on Method Overloading and Method Overriding

What is method overriding?

Modifying a super class method in the sub class is called method overriding. Using method overriding, we can change super class method according to the requirements of sub class.

What are the rules to be followed while overriding a method?

  • Must have the same argument list.
  • Must have the same return type, except that as of Java 5, the return type can be a subclass—this is known as a covariant return. 
  • Must not have a more restrictive access modifier.
  • May have a less restrictive access modifier.
  • Must not throw new or broader checked exceptions.
  • May throw fewer or narrower checked exceptions, or any unchecked exception.

Can we override static methods.? Explain the answer.

No, Static methods can not be overridden.
Explanation: Static method are not associated with any object.Even if we invoke any static method of the class on any object, at compile time, complier replaces the reference variable with the class name, therefore they can not be overridden.



What happens if we change the arguments of overriding method?

If we change the arguments of overriding method, then that method will be treated as overloaded not overridden.

Can we override protected method of super class as public method in the sub class?
Yes. We can change the visibility of overriding methods, but we can only increase it and can’t reduce it.

Can we change the return type of overriding method from Number type to Integer type?

Yes. The return type of the overriding method can be a sub-type of return type of super class(also knows as co-variance).

Can we override a super class method without throws clause as a method with throws clause in the sub class?

Yes, but only with unchecked type of exceptions.

Can we change an exception of a method with throws clause from NullPointerException to ArrayIndexOutOfBoundException while overriding it?

Yes. Overridden method may throw NullPointerException or it’s sub class exception or any unchecked type of exceptions.

What is the use of super keyword?

Using super keyword, we can refer super class version of overridden method in the sub class.

Can we override private methods?

No, we can not override private methods because private method is not visible in the sub class.

Can we remove throws clause of a method while overriding it?

Yes. You can remove throws clause of a method while overriding it.

What is method overloading?
When a class has more than one method with same name but different parameters, then we call those methods are overloaded. Overloaded methods will have same name but different number of arguments or different types of arguments.

What is method signature? What are the things it consist of?
Method signature is used by the compiler to differentiate the methods. Method signature consist of three things.
  •  Method name
  •  Number of arguments
  •  Types of arguments
Can we declare one overloaded method as static and another one as non-static?
Yes. Overloaded methods can be either static or non static.

How do compiler differentiate overloaded methods from duplicate methods?
Compiler uses method signature to check whether the method is overloaded or duplicated. Duplicate methods will have same method signatures i.e same name, same number of arguments and same types of arguments. Overloaded methods will also have same name but differ in number of arguments or else types of arguments.

Is it possible to have two methods in a class with same method signature but different return types?
No, compiler will give duplicate method error. Compiler checks only method signature for duplication not the return types. If two methods have same method signature, straight away it gives compile time error.

Can overloaded methods be synchronized?
Yes. Overloaded methods can be synchronized.
Can we overload main() method?
Yes, we can overload main() method. A class can have any number of main() methods but execution starts from public static void main(String[] args) only.

Can we declare overloaded methods as final?
Yes, we can declare overloaded methods as final.


Sunday, 9 October 2016

Understanding Polymorphism in Java

Polymorphism is one of the hot topics for a java interview. Questions, based on polymorphism, are asked at every level in java interview. In java, polymorphism is achieved, when a method with same name but different body are executed under different conditions.Polymorphic method invocations apply only to instance methods. You can always refer to an object with a more general reference variable type (a superclass or interface), but at runtime, the ONLY things that are dynamically selected based on the actual object (rather than the reference type) are instance methods. In java, objects can only be accessed through reference variables. Let us see few important things regarding the reference type variables in java :

  • A reference variable can be of only one type, and once declared, that type can never be changed (although the object it references can change).
  • A reference is a variable, so it can be reassigned to other objects, (unless the reference is declared final ).
  • A reference variable's type determines the methods that can be invoked on the object the variable is referencing.
  • A reference variable can refer to any object of the same type as the declared reference, or—this is the big one—it can refer to any subtype of the declared type!
  • A reference variable can be declared as a class type or an interface type. If the variable is declared as an interface type, it can reference any object of any class that implements the interface.
In java, there are two types of Polymorphism; 1. Runtime Polymorphism(implemented through method overriding). 2. Compile-time polymorphism(implemented through method overloading).

Method Overriding

When a method with same name and same signature is present in both Super class and Sub class, then the method in the super class is called over ridden method and the method in the sub class is called overriding method.Any time you have a class that inherits a method from a superclass, you have the opportunity to override the method. The key benefit of overriding is the ability to define behavior that's specific to a particular subclass type. The following example demonstrates a Horse subclass of Animal overriding the Animal version of the eat() method:

public class Animal {
	public void eat() {
		System.out.println("Eat method of Animal Class");
	}
}
class Horse extends Animal {
	public void eat() {
		System.out.println("Eat method of Horse class");
	}
}
public class TestAnimals 
{
	public static void main (String [] args) {
		Animal a = new Animal();
		Animal b = new Horse(); //Animal ref, but a Horse object
		a.eat(); // Runs the Animal version of eat()
		b.eat(); // Runs the Horse version of eat()
	}
}

In the above example,since,the object creation occurs at run time,therefore which version of eat() to be called is decided at run time.This why it is referred to as runtime polymorphism

Method Overloading

Two methods with same,name in same class but with different signatures will be referred to as as overloaded methods.Overloaded methods let you reuse the same method name in a class, but with different arguments (and optionally, a different return type).


class SumNumber {
	public int getSum(int x, int y) {
		return x + y;
	}
	// Overload the getSum method to add doubles instead of ints
	public double getSum(double x, double y) {
		return x + y;
	}
}

public class TestSum{
	public static void main(String [] args)
	{
		SumNumber sn = new SumNumber();
		System.out.println("Sum of Integer : "+sn.getSum(10, 20));//Integer version of getSum is called
		System.out.println("Sum of Double : "+sn.getSum(10.5, 19.5));//Double version of getSum is called

	}
}

In the above example,which method of get sum is to be called is decided at compile time,based on method signature therefore it is called compile-time polymorphism

Some key take aways and rules regarding method overloading and method overriding

  • Methods can be overridden or overloaded; constructors can be overloaded but not overridden.
  • Abstract methods must be overridden by the first concrete (non-abstract) subclass.
  • With respect to the method it overrides, the overriding method
  • Must have the same argument list.
  • Must have the same return type, except that as of Java 5, the return type can be a subclass—this is known as a covariant return.
  • Must not have a more restrictive access modifier.
  • May have a less restrictive access modifier.
  • Must not throw new or broader checked exceptions.
  • May throw fewer or narrower checked exceptions, or any unchecked exception.
  • final methods cannot be overridden.
  • Only inherited methods may be overridden, and remember that private methods are not inherited.
  • A subclass uses super.overriddenMethodName() to call the superclass version of an overridden method.
  • Overloading means reusing a method name, but with different arguments.
  • Overloaded methods must have different argument lists.
  • Overloaded methods may have different return types, if argument lists are also different.
  • Overloaded methods may have different access modifiers.
  • Overloaded methods may throw different exceptions.
  • Methods from a superclass can be overloaded in a subclass.
  • Polymorphism applies to overriding, not to overloading.
  • Object type (not the reference variable's type), determines which overridden method is used at runtime.
  • Reference type determines which overloaded method will be used at compile time.