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. 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
Q8. Does use of Final class enforces composition over Inheritance in Java ?
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. Yes , but only the read performance in case we don't need to modify it much. In case we need to modify it a lot , it creates write performance overheads as we would need to create many news objects instead of just changing one.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Immutable objects relieves us from the problems of inconsistencies and security and helps with better read performance but at the same time are write performance and storage overheads. In case any modification is required , a new object is created and thus creating multiple copies of it.
This is the reason we use StringBuffer / StringBuilder when we have to append some text multiple times and then create a String out of it.
Help us improve. Please let us know the company, where you were asked this question :
Ans. 1. Default definition for some of the methods , like equals, hashcode etc that gets carried to all objects even if you don't define anything. But that default definition is kind of like assigning 0 to integer, it just provide a safe state , nothing much for comparison.
2. Places where you have no idea about what object you may receive and just want to perform out of 8 basic methods of object class, something like printing their string representation ( defined by their toString method ), or equality ( using their equals method )
Help us improve. Please let us know the company, where you were asked this question :
Ans. We can store the references in a collection by adding to those objects in the collection. We can create a class "ObjectRegistry" with a collection or multiple collections with a search algorithm to look for the already collected objects.
Help us improve. Please let us know the company, where you were asked this question :
Ans. OOPs or Object Oriented Programming is a Programming model which is organized around Objects instead of processes. Instead of a process calling series of processes, this model stresses on communication between objects. Objects that all self sustained, provide security by encapsulating it's members and providing abstracted interfaces over the functions it performs. OOP's facilitate the following features
1. Inheritance for Code Reuse
2. Abstraction for modularity, maintenance and agility
3. Encapsulation for security and protection
4. Polymorphism for flexibility and interfacing
Help us improve. Please let us know the company, where you were asked this question :
Ans. Encapsulation is a feature of OOP's that binds the data and it's associated methods together as a single unit and facilitate protection and data hiding by providing minimal interface to outside. For example - member variables are declared private and are accessed through public methods. Moreover we have private methods that can only be used internally and hence providing minimal interface to outside class through use of public methods.
Ans. Java Programs are collection of objects that communicates with each other to get a task accomplished. To add to those objects, there are common spaces ( static i.e common for objects belonging to a class ) that are used too.
We can visualize objects as departments of an organization in real world. Just like Task gets initiated in one department and then files are moved across different departments to get work done. In a similar fashion, a task is initiated in one object ( having main method ) and then information ( through POJOs / DTOs ) is moved across objects to accomplish a task.
Help us improve. Please let us know the company, where you were asked this question :
LikeDiscussCorrect / Improve  objects  Java Programs are collection of objects  real life example of object communication
Ans. By encapsulating it within another class and declaring it private. In such a case, it will only be accessible through parent class or parent class object.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Now the reference points to a new object in memory. If that was the only reference for the previous object , it will be marked for garbage collection.
Foe example -
Object obj = new Object();
obj = new Object();
object created in first line will be eligible for garbage collection after line 2 as it looses all it's handlers.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Object is an entity in Java , i.e which has a state ( instance variables ) and methods attached to it ( static or non static , through class definition ). References are the identifiers that are used to point to objects.
For example -
Employee emp = new Employee();
emp = new Employee();
In this code, emp is the reference that gets assigned to the new object created by the new operator. In the second line , we have assigned the same reference to another object. So with these 2 lines of code, we have 2 objects in memory with reference emp now pointing to second object.
Help us improve. Please let us know the company, where you were asked this question :
Ans. It will print "true" with integers as well as strings. The reason is "Integer constant pool" and "String pool"
String pool maintains pool of string literals. When a string literal is used for the first time, a new string object is created and is added to the pool. Upon it's subsequent usage , the reference for the same object is returned. Similarly java uses integer constant pool.
Help us improve. Please let us know the company, where you were asked this question :