Top Hibernate Interview Questions and Answers

ObjectSets in Entity Framework (Reiterated)

In Entity Framework (prior versions), an ObjectSet represented a collection of entities associated with a database table or view. It allowed performing CRUD operations. Newer versions of Entity Framework utilize DbSet instead.

Entity States in Entity Framework

Entities in Entity Framework have states reflecting their lifecycle:

  • Added: New entity, not yet saved.
  • Deleted: Marked for deletion.
  • Modified: Changed; not yet saved.
  • Unchanged: Not modified.
  • Detached: No longer tracked by the context.

Comparable and Comparator Interfaces in Java

Comparable Comparator
Defines the natural ordering of objects of a class. Implemented within the class itself. Defines a custom comparison for objects. Implemented separately from the class.

Static Methods in Java (Reiterated)

A static method belongs to the class itself, not to any instance of the class. It's called using the class name (e.g., MyClass.myMethod()). static methods cannot directly access instance variables or call instance methods.

The volatile Keyword (Reiterated)

The volatile keyword in Java ensures that changes to a variable are immediately visible to all threads, preventing caching issues. It does not, however, guarantee atomicity for complex operations.

The finalize() Method (Reiterated)

The finalize() method is called by the garbage collector before an object is garbage collected. It's used for cleanup, but relying on it is generally discouraged; explicit resource management (using try-with-resources or other techniques) is preferred for reliable resource cleanup.

The `this` Keyword (Reiterated)

The this keyword in Java refers to the current instance of a class. It's used to access members (fields and methods) of the current object.

Assigning to `this`

You cannot assign a value to the this keyword; it's implicitly assigned and is a final reference.

Using `this` to Access Static Members

While possible, using this to access static members is not the recommended practice; static members are accessed directly using the class name.

Constructor Chaining with `this` (Reiterated)

Constructor chaining with this() allows a constructor to call another constructor within the same class, simplifying initialization.

Advantages of Passing `this` to a Method

  • Immutability of this.
  • Use in synchronized blocks.

Inheritance in Java (Reiterated)

Inheritance enables classes to inherit properties and methods from parent classes, promoting code reuse and establishing relationships (IS-A).

Reasons for Using Inheritance

  • Code reusability
  • Extensibility (adding functionality to existing classes).
  • Polymorphism (allowing objects of different classes to be treated as objects of a common type).

Superclass of All Classes in Java

All classes in Java implicitly extend the java.lang.Object class.

Why Java Doesn't Support Multiple Inheritance of Classes

Multiple inheritance (inheriting from more than one class) can lead to ambiguity (if two parent classes have the same method). Java allows multiple inheritance of interfaces, avoiding this problem.

Aggregation

Aggregation represents a "has-a" relationship; one class contains a reference to another, but the contained object can exist independently.

Composition

Composition is a stronger "part-of" relationship; the contained object cannot exist independently of the container.

Aggregation vs. Composition

Aggregation Composition
Weak relationship; contained object can exist independently. Strong relationship; contained object cannot exist independently.

Why Java Avoids Pointers

Direct pointer support is avoided in Java to enhance security and simplify memory management. They can be error-prone if not handled carefully.

The super Keyword (Reiterated)

The super keyword refers to the immediate parent (super) class. It's used to access members of the superclass, especially when they're overridden in the subclass.

Constructor Chaining with super()

The super() keyword calls the superclass constructor from the subclass constructor, initializing inherited fields.

Uses of super

  • Calling superclass constructors.
  • Accessing hidden superclass members.

this vs. super (Reiterated)

this super
Refers to the current object instance. Refers to the parent class object.

Method Overriding

Method overriding allows a subclass to provide a specific implementation for a method that is already defined in its superclass. It's a key aspect of polymorphism.

Overriding Static Methods

Static methods cannot be overridden.

Overriding Overloaded Methods

Overloaded methods can be overridden, but the signature must exactly match the overridden method.

Changing the Access Modifier of an Overridden Method

You can change the access modifier when overriding, but you cannot reduce the accessibility (e.g., you can make a protected method public, but not a public method private).

Modifying the throws Clause

You can modify the throws clause of an overridden method, but you cannot add checked exceptions that weren't in the superclass method.

Method Overriding Example

Java Code

// ... (code demonstrating method overriding would go here) ...
        

Virtual Functions in Java

All non-private, non-static methods in Java are virtual by default, enabling polymorphism.

Covariant Return Types

Since Java 5, overriding methods can have a return type that's a subtype of the superclass method's return type.