Interview Questions 1 - 10  «Prev Next»

Procedural Data Abstraction

Lesson 4 Procedural and Data Abstraction in Java SE 22
Objective Review modern Java SE 22 concepts of abstraction, encapsulation, and ADTs using updated terminology and examples.

Procedural and Data Abstraction (Modernized for Java SE 22)

This updated section revisits foundational object-oriented principles—abstraction, encapsulation, and information hiding—using Java SE 22 features such as records, sealed classes, pattern matching, and modules. These updates reflect how abstraction and encapsulation continue to evolve in the modern Java ecosystem.

  1. What is abstraction in Java SE 22?

    Answer:
    Abstraction allows developers to manage complexity by focusing on an object’s behavior rather than its implementation. In Java SE 22, abstraction is implemented through abstract classes, interfaces, and sealed types, which define clear boundaries for extension. The use of pattern matching for switch further enhances abstraction by allowing type-safe, expressive decision-making without exposing implementation details.

  2. What is procedural abstraction?

    Answer:
    Procedural abstraction separates what a method does from how it does it. In Java SE 22, developers use static and default interface methods to encapsulate behavior that can evolve independently of implementation. This approach promotes testable and reusable design patterns, aligning with functional-style programming through Stream operations and lambdas.

  3. What is data abstraction in modern Java?

    Answer:
    Data abstraction defines the essential characteristics of data while hiding unnecessary details. In Java SE 22, records provide a concise syntax for immutable data carriers that automatically encapsulate their fields. For mutable state, developers rely on well-defined accessors and the var keyword for readability without compromising encapsulation.


  4. What is the difference between the logical and physical views of an object?

    Answer:
    The logical view describes what an object represents—its public API and intended behavior—while the physical view concerns how the data is stored and implemented. In modern Java, encapsulation ensures that changes to the physical view (e.g., refactoring data fields or storage mechanisms) do not impact the logical interface exposed to clients.

  5. What is one advantage of procedural and data abstraction?

    Answer:
    They promote modularity and maintainability. Developers can modify implementation details inside a class or module without breaking code that depends on its public API. In Java SE 22, this principle is enforced through strong encapsulation in the module-info.java system, preventing unwanted access to internal packages.

  6. Why should higher-level classes access the DAO only through its methods?

    Answer:
    Limiting access through methods enforces loose coupling. In Java SE 22, DAOs often use record types for immutable transfer objects and sealed interfaces for defining extensible but controlled hierarchies. This ensures that persistence logic remains isolated and replaceable without rewriting higher-level business services.

  7. What is information hiding?

    Answer:
    Information hiding conceals internal logic to minimize interdependencies between components. In Java SE 22, modules (via the Java Platform Module System) enforce runtime boundaries that prevent external code from accessing non-exported packages. Combined with private and protected access modifiers, this ensures robust API design.

  8. What is the purpose of encapsulation in Java SE 22?

    Answer:
    Encapsulation binds data and behavior together, shielding internal details from external modification. A fully encapsulated class declares its fields as private and provides controlled access through methods or accessor records. In Java SE 22, records automatically generate accessors, and developers can override them for validation.

    Example:
    
    public record Account(String owner, double balance) {
        public Account {
            if (balance < 0) throw new IllegalArgumentException("Negative balance not allowed");
        }
    }
      


  9. What is an ADT (Abstract Data Type) in modern Java?

    Answer:
    An Abstract Data Type defines a contract for operations without specifying how they are implemented. In Java SE 22, an ADT can be modeled using an interface with sealed subtypes, allowing controlled extension. This pattern aligns with algebraic data modeling and enhances type safety.

    Example:
    
    sealed interface Shape permits Circle, Rectangle { }
    record Circle(double radius) implements Shape { }
    record Rectangle(double width, double height) implements Shape { }
      

  10. How do you access data within an Abstract Data Type?

    Answer:
    In Java SE 22, accessing ADT data is done through pattern matching and accessor methods rather than direct field manipulation. This ensures immutability and clarity.

    Example:
    
    Shape shape = new Circle(5.0);
    if (shape instanceof Circle(var r)) {
        System.out.println("Area = " + Math.PI * r * r);
    }
      

SEMrush Software