Java Interview Questions

Java Interview Questions
What are the important principles of object oriented concepts?

The three important OO principles are Encapsulation, Inheritance and polymorphism.
Explain the principle of encapsulation?

Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.
Explain polymorphism principle?

The meaning of Polymorphism is something like one name many forms. Polymorphism enables one entity to be used as general category for different types of actions. The specific action is determined by the exact nature of the situation. The concept of polymorphism can be explained as “one interface, multiple methods”.
What are the different forms of polymorphism in java?

The three form of polymorphism in java point of view:

Method overloading
Method overriding

What feature of java support multiple inheritance?

Java does not directly support multiple inheritance. The concept of interface is where multiple inheritance is achieved in java. Thus an interface contains only abstract methods and constants which could be implemented by java classes.
What is the difference between getClass() and instanceOf in java?

instanceOf is an operator that compares an object of specific type, where as getClass() method returns the runtime class of an object. It fetches the java instance of the given fully qualified type name. Thus the getClass() and instanceOf does not perform the same functionality.
Is it possible for an anonymous class to be defined to implement an interface or extend a class?

An anonymous class can implement an interface or extend a superclass but cannot be declared to do both.
What is the difference between == operator and equals () method?

The == operator is used to compare object references and not the contents of the object. Where as the equals () of object is used to compare the contents of the object.
What is the purpose of finalize () method?

Every class inherits the finalize() method from java.lang.Object. The garbage collector uses this method when it determines that no more references to the object exist. This method could be overridden to clean up non – java resource.
In a try-catch-finally statement what is the use of finally clause?

The finally clause is used to provide the capability of for the application to execute a piece of code no matter if the try block executed successfully or an exception was thrown or caught.