DEV Community

Cover image for πŸš€ Day 11 of My Automation Journey – Constructors, Inheritance & Java Interview Questions
bala d kaveri
bala d kaveri

Posted on

πŸš€ Day 11 of My Automation Journey – Constructors, Inheritance & Java Interview Questions

As part of my Java learning journey with my trainer, we were given a set of conceptual and tricky questions to answer.

The goal was not just to memorize answers but to understand Java fundamentals deeply, especially around:

Constructors
Inheritance
Java Features
var keyword
Object Creation

In this blog, I’m sharing the first set of questions along with explanations and code analysis.

1️⃣ How many ways can we initialize an instance variable in Java?

Instance variables can be initialized in multiple ways:

  1. Direct Initialization inside the class
class Example {
    int x = 10;
}
Enter fullscreen mode Exit fullscreen mode

Here the value is assigned directly during declaration.

  1. Using a Constructor
class Example {

    int x;

    Example() {
        x = 20;
    }
}
Enter fullscreen mode Exit fullscreen mode

When the object is created, the constructor assigns the value.

  1. Using a Method
class Example {

    int x;

    void setValue() {
        x = 30;
    }
}
Enter fullscreen mode Exit fullscreen mode

The value is initialized by calling a method.

  1. Using an Instance Initialization Block
class Example {

    int x;

    {
        x = 40;
    }
}
Enter fullscreen mode Exit fullscreen mode

This block runs before the constructor executes.

Key Concept

Instance variables belong to object memory (heap memory).

Every object gets its own copy of instance variables.

2️⃣ What is a Constructor in Java?

A constructor is a special method used to initialize objects when they are created.

Important Properties

Constructor name must be the same as the class name
It does not have a return type
It is automatically executed when an object is created
Used to initialize instance variables

Example

class Student {

    int id;

    Student(int id) {
        this.id = id;
    }

    public static void main(String[] args) {

        Student s1 = new Student(101);
        System.out.println(s1.id);

    }
}
Enter fullscreen mode Exit fullscreen mode

Code Explanation (Step by Step)
Step 1 – Class Loading

JVM loads the class Student.

Step 2 – Object Creation

Student s1 = new Student(101);
Enter fullscreen mode Exit fullscreen mode

This statement performs three operations.

1️⃣ Memory is allocated in Heap Memory
2️⃣ Constructor is called
3️⃣ Reference variable stores object address

Step 3 – Constructor Execution

Student(int id)
Enter fullscreen mode Exit fullscreen mode

The value 101 is passed to the constructor.

Step 4 – thiskeyword

this.id = id;
Enter fullscreen mode Exit fullscreen mode

Explanation:

Variable                     Meaning
this.id                          instance variable
id                           constructor parameter
Enter fullscreen mode Exit fullscreen mode

So Java assigns:

instance variable = parameter value
Step 5 – Printing Value

System.out.println(s1.id);
Enter fullscreen mode Exit fullscreen mode

Output:

101
Enter fullscreen mode Exit fullscreen mode

3️⃣ How to Achieve a Constructor? (Object Creation)

Constructors are executed when objects are created using the new keyword.

Example:

class Step1 {

    int x;

    Step1(int x) {
        this.x = x;
    }

    public static void main(String[] args) {

        Step1 ref1 = new Step1(10);

        System.out.println(ref1.x);

    }
}
Enter fullscreen mode Exit fullscreen mode

Deep Explanation of the Code
Line 1

Step1 ref1
Enter fullscreen mode Exit fullscreen mode

This is a reference variable.

It does not store the object itself β€” it stores the memory address of the object.

Line 2

new Step1(10)
Enter fullscreen mode Exit fullscreen mode

This does two things:

  1. Creates object
  2. Calls constructor

JVM Execution Flow

Step1 ref1 = new Step1(10);
Enter fullscreen mode Exit fullscreen mode

JVM internally performs:

  1. Allocate memory in Heap
  2. Call constructor
  3. Assign reference to ref1

Constructor Execution

Step1(int x)
Enter fullscreen mode Exit fullscreen mode

Value 10 is passed to constructor.

Then:

this.x = x
Enter fullscreen mode Exit fullscreen mode

Now instance variable becomes:

x = 10
Enter fullscreen mode Exit fullscreen mode

Final Output

10
Enter fullscreen mode Exit fullscreen mode

4️⃣ What is Constructor Overloading?

Constructor overloading means having multiple constructors in the same class with different parameters.

Example:

class Student {

    Student() {
        System.out.println("Default Constructor");
    }

    Student(int id) {
        System.out.println("Student ID: " + id);
    }

    public static void main(String[] args) {

        new Student();
        new Student(101);

    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Default Constructor
Student ID: 101
Enter fullscreen mode Exit fullscreen mode

Why Constructor Overloading?

It allows objects to be created in different ways.

Example:

Student()
Student(int id)
Student(int id, String name)
Enter fullscreen mode Exit fullscreen mode

5️⃣ Is Default Constructor Visible?

Yes.

If we do not define any constructor, the Java compiler automatically provides a default constructor.

Example:

class Test {

}
Enter fullscreen mode Exit fullscreen mode

Compiler internally creates:

Test() {

}
Enter fullscreen mode Exit fullscreen mode

6️⃣ How to Call a Constructor?

Constructors are called during object creation.

Example:

new ClassName();
Enter fullscreen mode Exit fullscreen mode

or

ClassName ref = new ClassName();

Example program:

class Demo {

    Demo() {
        System.out.println("Constructor Called");
    }

    public static void main(String[] args) {

        new Demo();

    }
}
Enter fullscreen mode Exit fullscreen mode

7️⃣ Real World Examples of Constructors
Bank Account

When creating a bank account:

Account number
Customer name
Balance

All values can be initialized using a constructor.

Hospital System

When registering a patient:

Patient name
Patient ID
Illness

These details can be initialized during object creation.

8️⃣ What is Inheritance?

Inheritance is a mechanism where one class acquires the properties and behaviours of another class.

This helps achieve code reusability.

Example

  • Parent Class β†’ Employee
  • Child Class β†’ Developer

Developer automatically inherits employee features.

9️⃣ Why Use Inheritance?

Inheritance helps with:

  1. Code Reusability
  2. Reduced Redundancy
  3. Easy Maintenance
  4. Better Code Structure

πŸ”Ÿ How to Achieve Inheritance?

Inheritance is implemented using the extends keyword.

Example:

class Parent {

}

class Child extends Parent {

}
Enter fullscreen mode Exit fullscreen mode

Here Child inherits properties from Parent.

1️⃣1️⃣ Real World Examples of Inheritance
Example 1 – Company Structure

Employee
   ↓
Developer
   ↓
Tester
Enter fullscreen mode Exit fullscreen mode

All share common employee properties.

Example 2 – Vehicle System

Vehicle
   ↓
Car
   ↓
Truck
Enter fullscreen mode Exit fullscreen mode

Common properties:

Engine
Speed
Fuel

1️⃣2️⃣ How many types of inheritance in Java?

Java supports 5 types of inheritance conceptually.

1️⃣3️⃣ Types of inheritance

  • Single Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Multiple Inheritance (via interfaces only)
  • Hybrid Inheritance (combination)

1️⃣4️⃣ Sample program for single inheritance

class Parent {

    String name;

}

class Child extends Parent {

    public static void main(String[] args) {

        Child obj = new Child();
        obj.name = "Rajiv";

        System.out.println(obj.name);

    }
}
Enter fullscreen mode Exit fullscreen mode

1️⃣5️⃣ Which inheritance is not supported in Java?

Multiple inheritance using classes is not supported in Java.

Reason:

It causes the Diamond Problem (Ambiguity problem).

Example conflict:

Class A β†’ method()
Class B β†’ method()
Class C extends A, B
Enter fullscreen mode Exit fullscreen mode

Java cannot decide which method to inherit.

1️⃣6️⃣ Sample program for multilevel inheritance

class Parent {

    String name = "Rajiv";

}

class Child extends Parent {

}

class Child2 extends Child {

    public static void main(String[] args) {

        Child2 obj = new Child2();
        System.out.println(obj.name);

    }
}
Enter fullscreen mode Exit fullscreen mode

1️⃣7️⃣ Latest Version of Java

The latest version of Java currently is Java 25 (released by Oracle as part of the Java SE platform).

Java releases two versions every year:

  • LTS (Long Term Support) versions
  • Non-LTS versions

Example LTS versions:

  • Java 8
  • Java 11
  • Java 17
  • Java 21

1️⃣8️⃣ Java Features

Some important features of Java include:

  • Simple – Easy to learn and use
  • Object-Oriented – Based on OOP concepts like class and objects
  • Platform Independent – "Write Once, Run Anywhere"
  • Secure – No direct memory access like pointers
  • Robust – Strong memory management and exception handling
  • Multithreaded – Supports multiple threads
  • Portable – Can run on different systems
  • Dynamic – Classes can be loaded dynamically

1️⃣9️⃣ Is Java 100% Object Oriented?

Java is not 100% object-oriented.

Reason:

Java supports primitive data types.

Example:

int
char
float
boolean

To overcome this, Java provides Wrapper Classes.

Example:

int β†’ Integer
char β†’ Character
Enter fullscreen mode Exit fullscreen mode

2️⃣0️⃣ What is var Keyword?

Introduced in Java 10.

It allows type inference for local variables.

Example:

var x = 10;
Enter fullscreen mode Exit fullscreen mode

Compiler automatically detects the type as int.

Restrictions of var:

var cannot be used for

  • Instance variables
  • Method parameters
  • Return types
  • Only allowed for local variables.

2️⃣1️⃣ Can We Run a Java Class Without main() Method?

Normally Java programs require the main() method because JVM starts execution from main().

However, there are special cases where a program can run without main():

Examples:

Static blocks (in older Java versions)

Applets
Servlets
JavaFX

But standard standalone Java programs must have a main() method.

2️⃣2️⃣ Do You Use var Keyword in Java?

Yes.

The var keyword is used to declare local variables with type inference.

Example:

var x = 10;
Enter fullscreen mode Exit fullscreen mode

Here the compiler automatically understands the type as int.

var can only be used for local variables inside methods.

2️⃣3️⃣ If We Create a Variable Using var, Where Will JVM Allocate Memory?

Since var can only be used for local variables, the memory is allocated in stack memory.

Example:

public static void main(String[] args) {
    var x = 10;
}

Enter fullscreen mode Exit fullscreen mode

x is a local variable, so it is stored in the stack.

2️⃣3️⃣ Can We Use var as a Global Variable?
The var keyword cannot be used for:

  • Instance variables
  • Static variables
  • Method parameters
  • Return types
  • It is allowed only inside methods (local variables).

Example ❌ Invalid:

class Test {
    var x = 10; // Error
}
Enter fullscreen mode Exit fullscreen mode

2️⃣5️⃣ What Is the Size of var Data Type?

var does not have a fixed size.

Because var is not a data type.

It is only a type inference keyword.

Example:

var x = 10;      // int (4 bytes)
var y = 10.5;    // double (8 bytes)
var name = "Java"; // String object
Enter fullscreen mode Exit fullscreen mode

The actual size depends on the inferred type.

2️⃣6️⃣ Which Company Releases Java Versions?

Originally Java was developed and released by Sun Microsystems.

Later Oracle Corporation acquired Sun Microsystems in 2010, and since then Oracle maintains and releases Java versions.

πŸ“Œ Key Learning from Day 11

Today’s discussion helped me understand:

  • Constructor fundamentals
  • Constructor overloading
  • Object creation flow
  • Inheritance basics
  • Java features
  • var keyword behavior

Understanding how JVM executes constructors and object creation is crucial for writing clean Java programs.

πŸ‘¨β€πŸ« Trainer: Nantha from Payilagam

πŸ€– A Small Note
I used ChatGPT to help structure and refine this blog while ensuring the concepts remain aligned with my trainer’s explanations.

Top comments (0)