Constructor & File Operations FAQ's
  • The constructor name must be the same as the class name.
  • A constructor does not have any return type, not even void.
  • We cannot call a constructor manually; it is implicitly called when an object is created.
  • Constructors support method overloading (multiple constructors with different parameters) but do not support method overriding.

A constructor is called implicitly when you create an object using the new keyword. You don’t need to call it manually—it runs automatically during object creation.

  • Default Constructor – No parameters, automatically provided by the compiler if none is defined.
  • Parameterized Constructor – Accepts arguments to initialize values during object creation.
  • Non-Parameterized Constructor – No parameters, explicitly defined by the programmer with custom logic.

Constructor Chaining is the process of calling one constructor from another constructor in the same class (this()) or from the parent class (super()). Its main use is to reuse constructor logic and avoid code duplication.

  • this: Refers to the current class object. Used to call current class methods and variables.
  • super: Refers to the parent class object. Used to call parent class methods and variables.
  • this(): Calls another constructor in the same class, must be the first statement in constructor, used for constructor chaining within class.
  • super(): Calls the parent class constructor, also must be the first statement in constructor, used to initialize parent class.

A Singleton is a design pattern that ensures only one instance of a class is created throughout the application. Useful when you want to control access to shared resources like database connections, file systems, or configuration settings.

  1. Static block runs first — when the class is loaded.
  2. Constructor runs next — when an object is created.
  3. Instance method runs last — when explicitly called using the object.

Answer: No, constructors are not inherited in Java. However, when we create an object of a child class, the parent class constructor is automatically called first using super().

This behavior may look like inheritance, but it is actually constructor chaining, not constructor inheritance.

No, constructors cannot be declared final because they are not inherited, and final is used to prevent inheritance or overriding. Therefore, it doesn't apply to constructors. Declaring a constructor as final will result in a compile-time error.

Yes, constructors can be overloaded by changing the number of arguments accepted or by changing the data type of the parameters.

No, constructors cannot be overridden because they are not regular methods and are not inherited by subclasses. Each class must define its own constructor.

No, constructors cannot be static. Constructors are used to create objects, but static means something belongs to the class itself, not to any object. So, using static with a constructor doesn't make sense. If you try it, the compiler will give an error.

  • createNewFile() – Create a new file
  • exists() – Check if file/directory exists
  • delete() – Delete a file or directory
  • getName() – Get the name of the file
  • getAbsolutePath() – Get full path of the file
  • listFiles() – List File objects inside directory