Core Java Interview Questions Guide
Core Java Interview Questions Guide
Method overloading occurs when multiple methods have the same name but different parameters within the same class, allowing different ways to handle similar operations. In contrast, method overriding involves a subclass redefining a method with the same name and parameters as a method in its superclass to provide specific functionality. Overloading is used for offering multiple ways to perform similar operations, while overriding is used for implementing specific behavior in subclasses .
Java does not support multiple inheritance to avoid complexity and ambiguity caused by the 'diamond problem'. Instead, it uses interfaces to achieve code reusability. By defining shared methods in interfaces, Java allows varying implementation in multiple classes, providing flexibility without inheritance-related ambiguities .
Java's access modifiers control encapsulation by restricting access to classes, methods, and variables. 'Public' allows complete access, 'protected' offers access within the same package and subclasses, 'package-private' (default) restricts access to only the package, and 'private' restricts access to the defining class. This ensures a controlled environment for data exposure, promoting encapsulation by providing varying degrees of access depending on the need .
A deadlock can be created in Java by having two or more threads holding locks while waiting indefinitely for another lock held by another thread. This can occur when threads acquire locks in different orders. To prevent deadlocks, it is essential to establish a consistent lock acquisition order and use timed locks or tryLock() methods that allow the thread to back off after a time-out .
Java achieves platform independence through the use of the Java Virtual Machine (JVM). The source code is compiled into bytecode, which can be executed on any device that has a compatible JVM, allowing Java programs to run on any platform without recompilation. This is known as 'write once, run anywhere' .
The synchronized keyword in Java ensures that only one thread can access a block or method at a time, providing thread safety. However, it can lead to reduced performance due to excessive locking and a potential for deadlocks. Synchronized code blocks can be less responsive because they increase the waiting time for threads .
Garbage collection in Java is an automatic process of reclaiming memory by deleting objects no longer reachable in the application. It prevents memory leaks by freeing up memory resources for new objects. This impacts memory management by optimizing available memory and improving program efficiency but may introduce occasional pauses during its execution .
The 'throws' keyword is used in a method signature to declare that a method can throw exceptions, allowing the caller to handle them. In contrast, 'throw' is used within a method to explicitly throw an exception. While 'throws' signals possible exception handling, 'throw' actively triggers an exception to signal a problem .
The 'final' keyword is used to declare constants or prevent inheritance or overriding. 'finally' is a block used to execute important code such as closing connections, which runs regardless of an exception. 'finalize' is a method called by the garbage collector when an object is about to be garbage collected, allowing resource cleanup. Each serves a unique purpose in ensuring program stability and resource management .
Heap memory is used for dynamic memory allocation where objects are stored, and it is shared among all threads. Stack memory is used for static memory allocation and stores local variables and method calls, which are thread-specific. Heap memory impacts performance due to slower access time, but provides more flexibility with memory allocation than stack memory, which has faster access time but limited space .