Wednesday, 11 December 2013

Java Programming interview questions

Sometimes in interviews you may asked to solve some programming questions based on the oops concept, so some questions related to the OOPs concepts are given below.

1.What would be the output of the given code snippets.

public static void methodA(Object o)
{
System.out.println("called from object");
}
public static void methodA(String s)
{
System.out.println("Called from string ");
}
public static void main(String[] a)
{
methodA(null);
}


So the answer would be
Called from string


because
Whenever there's Method Overloading, the JVM will search for the method from the most specific type to least specific type.


2.What would be the output of given program
class A
{
public void display()
{
System.out.println("class A");
}
}
public class B extends A
{
public void display()
{
System.out.println("class B");
}
public static void main(String[] args)
{
A a=new B();
a.display();
}
}



Ans:
class B

what happened here, we are calling the method from class A's reference variable but called the B's display method why?

This concept is called dynamic method dispatch, which is basically used by runtime environment for checking the actual type of objects.So here reference variable a contains the class B object actual so runtime called the method from class B.

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. Not static methods. Not variables. Only overridden instance methods are dynamically invoked based on the real object's type.

3.What would be the output of the given question.

class A
{
public void show()
{
System.out.println("hi");
}
}
public class B extends A
{
public void display()
{
System.out.println("hi display");
}
public static void main(String[] args)
{
A a=new B();
a.show();
a.display();
}
}


Ans.
Compile time error

But why we are getting compile time error a reference variable referring B class object and B class has both the method then why this error.
Reason for this error is that at compile time, compiler don't check the type of the Object referred by the reference variable a, compiler only see the reference variable's class and checks whether called method through this reference variable exist in the class or not,If not found error occurred.


4.Why Java does not support multiple inheritance.

As we know that multiple inheritance is not supported in Java but the question is Why they did not introduce multiple inheritance like C++.
Lets clear one thing what is multiple inheritance ?
Multiple Inheritance:- This is the capability which allow a class to extend more than one other class.

Note:- Inheritance is used for code re-usability.

The reason that Java's creators chose not to allow multiple inheritance is that it can become quite messy.

The problem is that if a class extended two other classes,and both super classes had, say, a doStuff() method, which version of doStuff() would the subclass inherit? This issue can lead to a scenario known as the "Deadly Diamond of Death" because of the shape of the class diagram that can be created in a multiple inheritance design. The diamond is formed when classes B and C both extend A, and both B and C inherit a method from A. If class D extends both B and C, and both B and C have overridden the method
in A, class D has, in theory, inherited two different implementations of the same method. Drawn as a class diagram, the shape of the four classes looks like a diamond.



So the compiler will be confused which version of doStuff() will be called, That's why multiple inheritance is not allowed in Java.


5. Some Rules for overriding a method


  • The argument list must exactly match that of the overridden method
  • The return type must be the same as, or a subtype of, the return type declared in the original overridden method in the superclass
  • The access level can't be more restrictive than the overridden method's. e.g You cann't use protected access level if superclass method have public access.
  • The access level CAN be less restrictive than that of the overridden method.
  • The overriding method CAN throw any unchecked (runtime) exception,regardless of whether the overridden method declares the exception.
  • The overriding method must NOT throw checked exceptions that are new or broader than those declared by the overridden method. For example, a method that declares a FileNotFoundException cannot be overridden by a method that declares a SQLException, Exception, or any other non-runtime exception unless it's a subclass of FileNotFoundException.
  • The overriding method can throw narrower or fewer exceptions. Just because an overridden method "takes risks" doesn't mean that the overriding subclass's exception takes the same risks. Bottom line: an overriding method doesn't have to declare any exceptions that it will never throw, regardless of what theoverridden method declares.
  • You cannot override a method marked final.
  • If a method can't be inherited, you cannot override it. Remember that overriding implies that you're reimplementing a method you inherited!
6.Rules for method overloading
Overloaded methods must change the argument list.
Overloaded methods can change the return type
Overloaded methods can change the access modifier
Overloaded methods can declare new or broader checked exception.
Remember one thing that the reference type not actual object type determines which overloaded method is invoked but which overridden version of the method to call is decided at runtime based on object type.
But which overloaded version of the method to call is based on the reference type of the argument list at compile time.

6.Why we used abstract class even if we have interfaces?

Both abstract class and interface are used to achieve abstraction in java.But what to choose is very good interview question. So abstract class is used where we want to provide some default behaviour , But interface is used where we are not providing any default behaviour , user can give any implementation for the behaviour of the interface .

One more thing that interface have all the method public if you want to make any method more restrictive then you can use abstract class.

But we also know that java doesn't provide multiple inheritance so its better to go for interface.


No comments:

Post a Comment