DEV Community

Rajesh Bhola
Rajesh Bhola

Posted on

Part 6 - Interfaces Interview Questions (Constructors, Object Creation & Parent Initialization)

Throughout this series, we've learned what interfaces are, how they work, and how they differ from abstract classes.

However, there are a few interview questions that confuse almost every Java beginner.

For example:

  • Why can't interfaces have constructors?
  • Why can abstract classes have constructors?
  • Does creating a child object also create a parent object?
  • What actually creates an object—the new keyword or the constructor?
  • Why does the parent constructor execute when creating a child object?

These questions frequently appear in Java interviews because they test your understanding of object creation, inheritance, and initialization rather than simple syntax.

Let's answer them one by one.


Why Can't an Interface Have a Constructor?

This is one of the most common Java interview questions.

To answer it, we first need to understand why constructors exist.


What Is the Purpose of a Constructor?

Many beginners think:

"A constructor creates an object."

This is incorrect.

A constructor does not create an object.

Its job is to initialize the object after it has been created.

The actual object creation is performed by the new keyword.


Object Creation Flow

Whenever we write:

Customer customer = new Customer();
Enter fullscreen mode Exit fullscreen mode

The JVM performs these steps:

Step 1
new keyword allocates memory

        ↓

Step 2
Object is created

        ↓

Step 3
Constructor executes

        ↓

Step 4
Object gets initialized

        ↓

Step 5
Reference variable points to the object
Enter fullscreen mode Exit fullscreen mode

This execution order is extremely important for interviews.


Why Doesn't an Interface Need a Constructor?

Interfaces cannot be instantiated.

Example:

interface PaymentService {

    void processPayment();

}
Enter fullscreen mode Exit fullscreen mode

Attempting to create an object:

PaymentService paymentService = new PaymentService();
Enter fullscreen mode Exit fullscreen mode

Compile-Time Error

PaymentService is abstract; cannot be instantiated
Enter fullscreen mode Exit fullscreen mode

Since objects of interfaces can never be created directly, there is nothing to initialize.

Therefore, constructors are unnecessary.


Why Can an Abstract Class Have a Constructor?

This question often surprises beginners.

After all, abstract classes also cannot be instantiated directly.

Example:

abstract class PaymentService {

    PaymentService() {

        System.out.println("PaymentService Constructor");

    }

}
Enter fullscreen mode Exit fullscreen mode

This compiles perfectly.

But why?


The Reason

Although we cannot create an object of an abstract class directly, its constructor executes whenever a child class object is created.

Example:

abstract class PaymentService {

    PaymentService() {

        System.out.println("PaymentService Constructor");

    }

}

class CreditCardPaymentService extends PaymentService {

    CreditCardPaymentService() {

        System.out.println("CreditCardPaymentService Constructor");

    }

}

public class Test {

    public static void main(String[] args) {

        new CreditCardPaymentService();

    }

}
Enter fullscreen mode Exit fullscreen mode

Output

PaymentService Constructor
CreditCardPaymentService Constructor
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Explanation

Step 1

Memory is allocated.

Step 2

The child object is created.

Step 3

The parent constructor executes first.

Step 4

The child constructor executes next.


Why Does the Parent Constructor Execute?

Because the child object inherits all instance variables from its parent.

The parent constructor initializes the parent portion of the child object.


Memory Representation

CreditCardPaymentService Object

+-----------------------------+
| Parent Instance Variables   |
+-----------------------------+
| Child Instance Variables    |
+-----------------------------+
Enter fullscreen mode Exit fullscreen mode

Before the child can initialize its own state, the inherited state must also be initialized.


Does Creating a Child Object Create a Parent Object?

This is one of the biggest misconceptions in Java.

The answer is:

No.

Only one object is created.

That object belongs to the child class.


Example

class Parent {

    Parent() {

        System.out.println(this.hashCode());

    }

}

class Child extends Parent {

    Child() {

        System.out.println(this.hashCode());

    }

}

public class Test {

    public static void main(String[] args) {

        Child child = new Child();

        System.out.println(child.hashCode());

    }

}
Enter fullscreen mode Exit fullscreen mode

Sample Output

1915318863
1915318863
1915318863
Enter fullscreen mode Exit fullscreen mode

Notice that all three values are identical.


Why Are All Hash Codes the Same?

Because there is only one object.

Both constructors execute on the same object.

Child Object
      │
      ├── Parent Constructor
      │
      └── Child Constructor
Enter fullscreen mode Exit fullscreen mode

No separate parent object is created.


Parent Constructor Execution Order

Whenever a child object is created:

new Child()

      │

      ▼

Memory Allocation

      │

      ▼

Parent Constructor

      │

      ▼

Child Constructor

      │

      ▼

Reference Returned
Enter fullscreen mode Exit fullscreen mode

This sequence never changes.


Interface vs Abstract Class (Constructor Perspective)

Feature Interface Abstract Class
Can have constructor ❌ No ✅ Yes
Can create object directly ❌ No ❌ No
Constructor executes ❌ Never ✅ During child object creation
Can contain instance variables ❌ No ✅ Yes
Initialization required ❌ No ✅ Yes

Why Doesn't an Interface Need Instance Variables?

Traditionally, interface fields are always:

  • public
  • static
  • final

Example:

interface AppConstants {

    int MAX_USERS = 100;

}
Enter fullscreen mode Exit fullscreen mode

These are class-level constants, not object state.

Since there are no instance variables to initialize, constructors serve no purpose.


Common Beginner Mistakes

Mistake 1: Thinking Constructors Create Objects

Incorrect.

The new keyword creates the object.

The constructor initializes it.


Mistake 2: Assuming Parent Objects Are Created

Creating a child object does not create a separate parent object.

Only one object exists.


Mistake 3: Believing Abstract Classes Cannot Have Constructors

They absolutely can.

Their constructors execute during child object creation.


Mistake 4: Thinking Interfaces Can Have Constructors

Interfaces define contracts, not object state.

They do not support constructors.


Best Practices

  • Remember that constructors initialize objects—they do not create them.
  • Use abstract class constructors to initialize common state shared by subclasses.
  • Prefer interfaces for defining contracts, not storing object state.
  • Keep constructors simple and focused on initialization.
  • Avoid placing complex business logic inside constructors.

Interview Questions

1. Why can't interfaces have constructors?

Because interfaces cannot be instantiated and therefore have no object state to initialize.

Why interviewers ask

To test your understanding of object creation and interface design.


2. Why can abstract classes have constructors?

Their constructors initialize the inherited portion of child objects during object creation.


3. Does creating a child object create a parent object?

No.

Only one child object is created.

The parent constructor simply initializes the parent part of that object.

Common trap

Many candidates incorrectly answer that two objects are created.


4. What creates an object?

The new keyword creates the object.

The constructor initializes it.


5. Which constructor executes first?

The parent constructor always executes before the child constructor.


6. Why are interface fields typically public static final?

They represent shared constants rather than object state, so no constructor is needed to initialize them.


Quick Memory Trick 🧠

Remember NCPI:

N → new creates object

C → Constructor initializes object

P → Parent constructor runs first

I → Interface has no constructor
Enter fullscreen mode Exit fullscreen mode

Whenever you're asked about object creation, think NCPI.


Key Takeaways

  • The new keyword creates objects.
  • Constructors initialize objects after creation.
  • Interfaces cannot have constructors because they cannot be instantiated.
  • Abstract classes can have constructors.
  • Parent constructors execute before child constructors.
  • Creating a child object does not create a separate parent object.
  • Constructors help initialize inherited state in child objects.
  • Understanding object creation order is essential for Java interviews.

If you found this guide helpful, leave a ❤️ and follow for more beginner-friendly Java tutorials, interview questions, and practical coding examples.

Happy Coding!

Top comments (1)

Collapse
 
speed_engineer profile image
speed engineer

Really clear series - the "new creates, constructor initializes" distinction trips up almost everyone, so good to hammer it. Two additions for the interview version, both building straight on your "parent runs first" point:

  1. WHY it runs first: the compiler inserts an implicit super() as the first line of every constructor (unless you write super(...) or this(...) yourself). That's the actual mechanism - and a common follow-up trap: if the parent has no no-arg constructor, that implicit super() won't compile, and the child is forced to call super(args) explicitly.

  2. The gotcha that falls straight out of "parent before child": never call an overridable method from a constructor. The parent ctor runs before the child's field initializers, so if it calls a method the child overrides, it dispatches to the child version while the child's fields are still null/0:

class Parent { Parent(){ init(); } void init(){} }
class Child extends Parent {
    String name = "x";
    void init(){ System.out.println(name.length()); } // NPE
}
Enter fullscreen mode Exit fullscreen mode

new Child() throws NPE - name is still null when Parent's constructor calls init(). Effective Java Item 19, and a real interview favorite.

Modern note: since Java 8, interfaces can have default/static methods (and private since 9), so they aren't purely abstract contracts anymore - but your core point holds: still no instance state, no constructor, nothing per-object to initialize.