More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know.
Ans. Yes, we can do that. Compiler wont complain. But using object reference we can only access methods which have been defined for object class i.e clone(), equals(), hashCode(), toString() etc.
We cannot access methods defined in String class or in any class in hierarchy between String and Object.
For example - we cannot do obj.append("abc") as it will now give compile time error.
Help us improve. Please let us know the company, where you were asked this question :
Q34. How does java identifies which method to be called in method overriding or runtime polymorphism, when both methods share the same name and signature ?
Ans. Yes, to a certain extent. But the objective for Final class could be beyond just enforcing composition as certain classes might have been created without inheritance or composition in mind.
Help us improve. Please let us know the company, where you were asked this question :
Ans. 1. Enforcing composition over inheritance
2. Restricting overriding of certain methods
3. Final methods are faster than regular instance methods
4. Enforcing Immutability
Help us improve. Please let us know the company, where you were asked this question :
LikeDiscussCorrect / Improve  final class   reasons for final class   restricting inheritance  object oriented programming (oops)  oops concepts expert
Q38. Difference between Class#getInstance() and new operator ?
Ans. Class.getInstance doesn't call the constructor whereas if we create an object using new operator , we need to have a matching constructor or copiler should provide a default constructor.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Object is a run time entity whose state is stored in fields and behavior is shown via methods. Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Java expects the superclass ( Object Class ) constructor to be called while creation of any object. So super constructor is called in case there are no instance variables to initialize.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Yes but as Hibernate creates the Proxy Classes inherited from the Entity Classes to communicate with Database for lazy initialization. Declaring entity classes as final will prohibit communication with database lazily and hence will be a performance hit.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Stateless objects are the objects without instance fields (instance variables). The class may have compile time constants i.e static final fields.Immutable objects are the objects which have state but the state cannot be changed after initialization. Both are Thread safe.
Help us improve. Please let us know the company, where you were asked this question :
Q48. Which of the following do you think is the primary reason you would never use a static class even the application doesn't need multiple requests or threads ?
a. Serialization
b. Runtime Polymorphism
c. Lazy Loading
d. Memory
Ans. Runtime Polymorphism
Help us improve. Please let us know the company, where you were asked this question :
LikeDiscussCorrect / Improve  static class   static vs singleton   java   oops   objects  Runtime Polymorphism
Must know at all levels. Among Top 10 frequently asked questions in Java. Very frequently asked to fresh graduates or less experienced professionals.
Ans. Static polymorphism is the polymorphic resolution identified at compile time and is achieved through function overloading whereas dynamic polymorphism is the polymorphic resolution identified at runtime and is achieved through method overriding.
Help us improve. Please let us know the company, where you were asked this question :
Ans. There was no multiple inheritance in java before version 8 as implementing multiple interfaces cannot be termed as inheritance.
With Java 8 , came the concept of default methods wherein the class implementing the interface carries the default implementation from the interface and hence facilitating multiple inheritance.
Help us improve. Please let us know the company, where you were asked this question :
Q53. Can a lock be acquired on a class? How is it different than the object level lock ?
Ans. Yes, a lock can be acquired on the class. Class level lock is applicable on the whole class and hence on all objects of the class whereas the object level lock is applicable on the respective object.
Help us improve. Please let us know the company, where you were asked this question :
Ans. DTO is Data Transfer Object i.e Pojo which is supposed to move between different layers of software architecture for transferring information. Example could be the object passed from client in case of web service call or the object passed to Persister for Database Persistence.
Help us improve. Please let us know the company, where you were asked this question :
Ans. DAO is Data Access Object which is used within Persistence Layer to make Database Transaction. For example , the class holding the Database connection and CRUD methods.
Help us improve. Please let us know the company, where you were asked this question :
Ans. POJO is Plain Java Object that holds only Member Elements , getters are setters and minimal processing on that. The primary purpose of such object is to either transfer information or keeping state for a while. They are not intended to provide any processing or transformation.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Multi Level Inheritance is multi level hierarchy of classes. Here is the example - http://www.buggybread.com/2015/09/java-se-class-diagram-classes-that_603.html
Class RoleList extends ArrayList which in turn extends AbstractList which in turn extends AbstractCollection.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Default methods results in multiple inheritance of behavior and not of state. In case we try to implement multiple interfaces with default method having same name and signature, and don't override it in implementation class, it will throw an error.
For example -
interface MyInterface {
public void default myMethod(){
}
}
interface MyInterface2 {
public void default myMethod(){
}
}
class MyClass implements MyInterface,MyInterface2 {
}
This code will compilation error "Duplicate Default Method"
if we specify the definition of myMethod() in myClass, compiler won't complain and there is no conflict and MyClass can use overridden definition. But if we don't override myMethod() in MyClass, Java would be in conflict as to what definition should be carried to MyClass and hence throws compilation error.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Copy Constructor is used for creating a duplicate of another object. Duplicate object will have the same state of the copied object but will have independent values in different memory locations. Copy Constructor can be useful to create duplicates of immutable objects as the Original cannot be tampered. Moreover It can be useful if base copies are required for individual requests in threading.
Help us improve. Please let us know the company, where you were asked this question :