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. Abstract classes can take care of that to a certain extent. Though they are little heavier than Interfaces but An abstract class with all abstract methods and no instance variables will be able to help with everything that currently an interface does.
The only problem is that a class can only extend one class whereas it can implements multiple interfaces and that is the reason Interfaces were introduced in Java, i.e to get over the problem of multiple inheritance.
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
Q11. 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 :
Q15. Write a method that takes a string as input and identifies if any permutation of the String is Palindrome ? For example - ttoobb has a permutation 'bottob' which is a palindrome.
Q17. Why do we need to specify import statement ? Don't you think Java could have been designed in such a way to automatically import everything that's in the class path ?
Ans. Java could design it in such a manner but would result in ambiguity if there are multiple files with the same name. The only way to get over it to show the message when you use any such class to provide explicit import with the package prefix. The other problem could be that Java might have to change the early import to Late import and check what's being used to decide what needs to be imported as otherwise you will see errors regarding the ambiguous imports with duplicate file name.
Help us improve. Please let us know the company, where you were asked this question :
Q20. What are the advantage of Abstract classes over interfaces with respect to Java 7 ? and What changed in Java 8 to help facilitate that in Java 8 ?
Ans. Abstract Classes provide default implementations of methods that are inherited by the classes that extend them, which was not the case for Interfaces. This changed in Java 8, where default implementations are provided for methods.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Yes , a gc method to explicitly call garbage collection immediately on the object. It will provide better flexibility over application that require optimal usage of memory.
Help us improve. Please let us know the company, where you were asked this question :
Ans. String is widely used as parameter for many java classes, e.g. network connection, opening files, etc. Making it mutable might possess threats due to interception by the other code segment or hacker over internet.
Once a String constant is created in Java , it stays in string constant pool until garbage collected and hence stays there much longer than what's needed. Any unauthorized access to string Pool pose a threat of exposing these values.
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. Java provides a default constructor ( no argument ) if no constructor is provided by the programmer. Java makes a check during pre compilation if there is any constructor provided by the programmer. If any constructor is provided, Java understands that programmer has provided all the mechanism for initialization and construction of the object and may not even intend to create objects by calling a no argument constructor. For example - One may like to have objects created with self defined state otherwise restrict its creation.
We make the constructor private if we want to make a class singleton as we would like to restrict creation of new objects using new operator and default constructor. Similar could be the situation wherein we would like to construct objects in a particular manner only.
Help us improve. Please let us know the company, where you were asked this question :
Ans. It's weird that compiler doesn't complain if we declare transient with static variable because it makes no sense. At least a warning message saying "transient is useless in this situation" would have helped with code cleaning.
Static variables are never serialized and transient is an indication that the specified variable shouldn't be serialized so its kind of double enforcement not to serialize.
It could be that as it makes no different to the variable behavior and hence using both keywords with a variable are permitted.
Help us improve. Please let us know the company, where you were asked this question :
1.Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer,
2.If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
3.It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Everytime an object is serialized the java serialization mechanism automatically computes a hash value by passing the meta information for the class. This id is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization
Help us improve. Please let us know the company, where you were asked this question :