Java Questions  «Prev  Next»

Java Interview Questions and Answers

  1. In Java SE 22, can a private method of a parent class be declared within a subclass?

    Answer:
    In Java SE 22 (and all earlier versions of Java), the rules about `private` methods are consistent:
    Example
    
    class Parent {
        private void display() {
            System.out.println("Parent display()");
        }
    }
    
    class Child extends Parent {
        // This is NOT an override, just a new method in Child
        private void display() {
            System.out.println("Child display()");
        }
    }
    

    • Here, `Parent.display()` and `Child.display()` are two completely separate methods.
    • The `Child` method does not override the parent’s method, because the parent’s method is private and not inherited.

    Key Point
    • ✅ Yes, a subclass can declare its own private method with the same name as a parent class’s private method.
    • ❌ But this does not mean it is declaring or overriding the parent’s private method, it is simply a new method that belongs only to the subclass.


  2. Why doesn’t Java support multiple inheritance of classes?

    Java avoids multiple inheritance of classes to prevent the “diamond problem,” where ambiguity arises if two parent classes define the same method. Instead, Java allows multiple inheritance through interfaces. Since Java 8, interfaces can also provide default and static methods, enabling flexible code reuse without the risks of class-level multiple inheritance.

  3. What is the difference between final, finally, and finalize()?

    • final — applied to variables (makes them constants), methods (prevents overriding), and classes (prevents inheritance).
    • finally — a block used in exception handling that always executes, typically for cleanup.
    • finalize() — a method called by the garbage collector before reclaiming an object. Deprecated in Java 9 and removed in Java 18; developers should use try-with-resources or Cleaner API instead.

  4. Where and how can you use a private constructor?

    A private constructor is used to restrict object creation from outside the class. Common use cases include:
    • Singleton Pattern: Ensures only one instance of a class exists.
    • Utility Classes: Prevents instantiation (e.g., java.util.Collections).
    • Factory Methods: Creation is controlled through a public static method.

  5. In System.out.println(), what do System, out, and println() represent?

    • System — a final class in java.lang providing system-level utilities.
    • out — a static field of type PrintStream within System, representing the standard output stream.
    • println() — a method of PrintStream that prints text and moves to a new line.

  6. What is meant by "Abstract Interface"?

    This is a misleading term. All interfaces in Java are inherently abstract — you cannot instantiate them. Since Java 8, interfaces can contain default methods with implementations and static methods. From Java 9 onward, they can also contain private methods to share common code internally.

  7. Can you make an instance of an abstract class?

    No. Abstract classes cannot be directly instantiated. They must be subclassed, and the subclass must provide implementations for the abstract methods. For example, java.util.Calendar provides the static getInstance() method that returns a concrete subclass instance.

  8. What is the result of a ternary operator expression like x < y ? a : b = p * q?

    This syntax is invalid. The correct ternary operator form is: result = (x < y) ? a : (p * q); If x < y evaluates to true, a is chosen; otherwise, p * q is chosen. Always check parentheses and operator precedence when using ternary expressions.


  9. What is the difference between Swing, AWT, and modern Java UI frameworks?

    • AWT: heavy-weight components that rely on the native OS GUI toolkit.
    • Swing: — lightweight, platform-independent components built on top of AWT.
    • JavaFX: — the modern UI toolkit for Java, supporting FXML, CSS styling, and hardware-accelerated graphics.
    Today, JavaFX is the preferred choice for new Java desktop applications.

  10. Why doesn’t Java support pointers?

    Java omits explicit pointer arithmetic to improve security and reliability. Instead, it uses references to objects, which act like safe pointers but are managed by the JVM. This design prevents memory corruption and buffer overflows common in pointer-heavy languages like C/C++.

  11. How do DOM and SAX XML parsers differ?

    DOM (Document Object Model)SAX (Simple API for XML)
    Parses entire XML into a tree of objects.Processes XML sequentially as events.
    High memory usage; suited for small to medium documents.Low memory usage; suited for large documents.
    Slower for very large files due to full in-memory representation.Faster for streaming and partial parsing.
    Easy navigation (random access supported).No backward navigation; forward-only.
    Simpler to manipulate XML structure programmatically.Requires custom code for building object structures.

SEMrush Software