What are the 3 types of constructor?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Understanding the different types of constructors is essential in Object-Oriented Programming (OOP) as they play a crucial role in initializing objects. Constructors ensure that objects are created in a valid state and can be customized based on specific requirements. Here are the three main types of constructors you should be familiar with for your interviews.

Default Constructor

A default constructor is a constructor that takes no arguments. If no constructor is explicitly defined in a class, most programming languages automatically provide a default constructor. This constructor initializes the object with default values.

Example

Consider a Book class in Java:

public class Book { private String title; private String author; // Default constructor public Book() { this.title = "Unknown Title"; this.author = "Unknown Author"; } public void displayInfo() { System.out.println("Title: " + title + ", Author: " + author); } } // Usage public class Main { public static void main(String[] args) { Book defaultBook = new Book(); defaultBook.displayInfo(); // Outputs: Title: Unknown Title, Author: Unknown Author } }

In this example, the Book class has a default constructor that sets the title and author to default values. When a new Book object is created without any parameters, the default constructor is invoked.

Parameterized Constructor

A parameterized constructor allows you to initialize an object with specific values by passing parameters to the constructor. This provides greater flexibility and control over the object's initial state.

Example

Continuing with the Book class:

public class Book { private String title; private String author; // Parameterized constructor public Book(String title, String author) { this.title = title; this.author = author; } public void displayInfo() { System.out.println("Title: " + title + ", Author: " + author); } } // Usage public class Main { public static void main(String[] args) { Book specificBook = new Book("1984", "George Orwell"); specificBook.displayInfo(); // Outputs: Title: 1984, Author: George Orwell } }

Here, the Book class has a parameterized constructor that accepts title and author as parameters. This allows the creation of Book objects with specific values provided at the time of instantiation.

Copy Constructor

A copy constructor creates a new object as a copy of an existing object. This type of constructor is particularly useful when you need to duplicate objects without affecting the original instance.

Example

In C++, a copy constructor can be defined as follows:

#include <iostream> #include <string> class Book { private: std::string title; std::string author; public: // Parameterized constructor Book(std::string t, std::string a) : title(t), author(a) {} // Copy constructor Book(const Book &b) { title = b.title; author = b.author; } void displayInfo() { std::cout << "Title: " << title << ", Author: " << author << std::endl; } }; int main() { Book originalBook("To Kill a Mockingbird", "Harper Lee"); Book copiedBook = originalBook; // Invokes copy constructor copiedBook.displayInfo(); // Outputs: Title: To Kill a Mockingbird, Author: Harper Lee return 0; }

In this C++ example, the Book class has a copy constructor that takes a reference to another Book object and copies its title and author attributes. When copiedBook is created as a copy of originalBook, the copy constructor is invoked to duplicate the object's state.

To further enhance your understanding of constructors and other OOP concepts, consider enrolling in the following courses from DesignGurus.io:

These courses provide comprehensive insights and practical examples to help you master Object-Oriented Programming principles and excel in your technical interviews.

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
How to explain gaps in your resume during interviews?
What is endl in C++?
Can a single core handle multiple threads?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
Grokking Advanced Coding Patterns for Interviews
Master advanced coding patterns for interviews: Unlock the key to acing MAANG-level coding questions.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.