Monday, August 19, 2013

Java Interview Questions

What is the difference between abstraction and encapsulation?
  • Abstraction focuses on the outside view of an object (i.e. the interface) Encapsulation (information hiding) prevents clients from seeing it's inside view, where the behavior of the abstraction is implemented.
  • Abstraction solves the problem in the design side while Encapsulation is the Implementation.
  • Encapsulation is the deliverables of Abstraction. Encapsulation barely talks about grouping up your abstraction to suit the developer needs.
  • Explain the user defined Exceptions?
    User defined Exceptions are the separate Exception classes defined by the user for specific purposed. An user defined can created by simply sub-classing it to the Exception class. This allows custom exceptions to be generated (using throw) and caught in the same way as normal exceptions.
    Example:

    class myCustomException extends Exception {
    / The class simply has to exist to be an exception
    }
    Can we declare a class as Abstract without having any abstract method?
    Yes we can create an abstract class by using abstract keyword before class name even if it doesn't have any abstract method. However, if a class has even one abstract method, it must be declared as abstract otherwise it will give an error.
    Can overloaded methods be override too?
    Yes, derived classes still can override the overloaded methods. Polymorphism can still happen. Compiler will not binding the method calls since it is overloaded, because it might be overridden now or in the future.
    What would you use to compare two String variables - the operator == or the method equals()?
    The method equals() to compare the values of the Strings and the == to check if two variables point at the same instance of a String object.
    What is Dynamic Binding?
    Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding (also known as late binding) means that the code associated with a given procedure call is not known until the time of the call at run-time. It is associated with polymorphism and inheritance.

    No comments:

    Post a Comment