Wisdom
  • Welcome
  • core
    • Flyway
    • Bean Validation
    • Lombok
    • Webclient
      • Generic Webclient Api
      • SSL Certificate
    • Application Event Publisher
    • REST API's Design
      • Http Methods and Status Codes
      • Resource Naming and URL Structure
      • Request / Response Design
      • Versioning Strategies
      • Filtering and Searching In API
    • Spring Boot Mail Integration
      • Sendgrid
    • Template Engines
      • Java Template Engine [JTE]
  • security
    • Complete Guide to URL Matchers in Spring Security: Types, Examples, Pros, Cons, and Best Use Cases
    • Passwordless Login With Spring Security 6
      • Spring Security OneTimeToken
        • One Time Token with default configuration
        • One Time Token with custom configuration
        • One Time Token With Jwt
  • others
    • How to Integrate WhatsApp for Sending Messages in Your Application
  • java
    • Interview Questions
      • Constructor
      • Serialization
      • Abstract Class
    • GSON
      • Type Token
      • Joda Datetime Custom Serializer and Deserializer
  • Nginx
    • Core Concepts and Basics
    • Deep Dive on NGINX Configuration Blocks
    • Deep Dive on NGINX Directives
    • Deep Dive into Nginx Variables
    • Nginx as a Reverse Proxy and Load Balancer
    • Security Hardening in NGINX
    • Performance Optimization & Tuning in NGINX
    • Dynamic DNS Resolution in Nginx
    • Advanced Configuration & Use Cases in NGINX
    • Streaming & Media Delivery in NGINX
    • Final Configuration
  • Angular
    • How to Open a PDF or an Image in Angular Without Dependencies
    • Displaying Colored Logs with Search Highlighting in Angular 6
    • Implementing Auto-Suggestion in Input Field in Angular Template-Driven Forms
    • Creating an Angular Project Using npx Without Installing It Globally
    • Skip SCSS and Test Files in Angular with ng generate
  • Javascript
    • When JavaScript's Set Falls Short for Ensuring Uniqueness in Arrays of Objects
    • Demonstrating a Function to Get the Last N Months in JavaScript
    • How to Convert Numbers to Words in the Indian Numbering System Using JavaScript
    • Sorting Based on Multiple Criteria
  • TYPESCRIPT
    • Using Omit in TypeScript
Powered by GitBook
On this page
  • What is a Constructor in Java?
  • What are the Rules for defining a constructor?
  • What is the default constructor?
  • What keywords are applicable for constructors?
  • Why constructor not return any value?
  • Why constructor definition should not be static?
  • Can we inherit constructors? Why is constructor not inherited?
  • What is the purpose of the default constructor?
  • Difference between Method and Constructor
  • What is the access modifier of the Default constructor?
  • Can constructor be synchronized in Java?
  • Can we throw exceptions from a constructor?
  • What are the access modifiers that cannot be applied to a constructor?
  • Can we declare a constructor as private? Use of private Constructor.
  • What is constructor chaining?
  • How do I call subclass constructor from superclass constructor?
  • Do we have destructors in Java?
  • Why compiler given constructor is called as default constructor?
  • Can you create an object without using new operator in Java?
  • Why can constructors not be final in Java?
  • Why can constructors not be abstract in Java?
  • Why can constructors not be static in Java?
  1. java
  2. Interview Questions

Constructor

What is a Constructor in Java?

Constructor is just like a method in Java that is used to initialize the state of an object and will be invoked during the time of object creation.

What are the Rules for defining a constructor?

  • Constructor name should be the same as the class name

  • It cannot contain any return type

  • It can have all Access Modifiers are allowed (private , public, protected, default)

  • It Cannot have any Non Access Modifiers (final ,static, abstract, synchronized)

  • No return statement is allowed

  • It can take any number of parameters

  • Constructor can throw exception, we can have throws clause

What is the default constructor?

Used to initialize with default values, which will be provided by JVM if programmer is not defining.

What keywords are applicable for constructors?

public , private , protected , default (if we care not mentioning )

Why constructor not return any value?

Constructor will never return any value even void, because the basic aim constructor is to place value in the object. If it will return value then it will become method .

Why constructor definition should not be static?

Constructor definition should not be static because constructor will be called each and every time when object is created. If we made it static then every time object will be created automatically.

Can we inherit constructors? Why is constructor not inherited?

No constructors is not inherited because every class constructor is created for initialize its own data members. The scope of constructor is within the class. So we can’t inherit them.

What is the purpose of the default constructor?

The purpose of default constructor is to create multiple object with respect to same class for placing same value. The default constructor provides the default values to the objects. The java compiler creates a default constructor only if there is no constructor in the class.

Difference between Method and Constructor

Method
Constructor

Method can be any user defined name

Constructor must be class name.

Method should have return type

It should not have any return type (even void)

Method should be called explicitly either with object reference or class reference

It will be called automatically whenever object is created

Method is not provided by compiler in any case.

The java compiler provides a default constructor if we do not have any constructor.

What is the access modifier of the Default constructor?

It it depends on modifier of class . If the Class declared as public, then the default constructor access modifier will be public.

Can constructor be synchronized in Java?

No. constructor cannot be synchronized.

Can we throw exceptions from a constructor?

Yes, constructor can have throws clause.

What are the access modifiers that cannot be applied to a constructor?

final ,static, abstract and synchronized keywords cannot be applied to a constructor.

Can we declare a constructor as private? Use of private Constructor.

Yes, We can declare constructor as private but if you make any constructor as private then you cannot create object of that class from outside of class. We can also use other access modifiers with constructor like default, public, protected.

What is constructor chaining?

Constructor Chaining is a technique of calling another constructor from one constructor. this() is used to call same class constructor, while super() is used to call super class constructor.

How do I call subclass constructor from superclass constructor?

No. This is not possible.

Do we have destructors in Java?

There is no destructors in java because java is garbage collected language.

Why compiler given constructor is called as default constructor?

  • Because it obtain all its default properties from its class.

  • Its accessibility modifier is same as the class .

  • Its name same as the class .

  • It does not have parameter and logic .

Can you create an object without using new operator in Java?

Yes, We can create an object without using new operator. Other ways are

//a) Using newInstance() Method
     Class c = Class.forName("packageName.MyClass");
     MyClass object = (MyClass) c.newInstance();

//b) Using clone() method.
     MyClass object1 = new MyClass();
     MyClass object2 = object1.clone();

//c) Using object deserialization
     ObjectInputStream inStream = new ObjectInputStream(anInputStream );
     MyClass object = (MyClass) inStream.readObject();
//d) Creating string and array objects :
     String s = “hello”;
     int a[] = {1,2,3,4};

Why can constructors not be final in Java?

Why can constructors not be abstract in Java?

When you set a method as abstract, then “The method doesn’t or cannot have a body”. A constructor will be automatically called when an object is created. It cannot lack a body, moreover an abstract constructor could never be implemented.

Why can constructors not be static in Java?

When you set a method as static, it means “The Method belong to class and not to any particular object” but a constructor is always invoked regarding an object, so it makes no sense for a constructor to be static.

PreviousInterview QuestionsNextSerialization

Last updated 5 months ago

When you set a method as final, then " The method cannot be overridden by any class" , but Constructor by JLS ( ) definition can’t be overridden. A constructor is not inherited, so there is no need for declaring it as final.

Java Language Specification