• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

2 Types of Constructors in Java | Example | Constructor Overloading

Pranati Paidipati/16374/0
CodeJAVA

As a part of the Java tutorial series, in the previous tutorial, we learned about classed and objects in Java.

This short tutorial will help you understand the basics about constructor and, types of constructors in Java. I also explain to you – What are the basics rules for writing constructor in Java? And what is constructor overloading in Java? Explained with an example.

So let’s begin.

What is Constructor in Java?

A constructor is a block of code which gets called when an instance of the object is defined and a portion of memory is assigned to it. In simple words, a constructor is a method that is employed to initialize an object that is made with the help of a new keyword.

Make a note of the fact that it is not a compulsion to write a constructor for a particular class as the compiler creates a constructor by default for at runtime.

The basic rule of creating constructor is that its name must be the same as that of the class name with no return type.

Types Of Constructors in Java:

Java defines 2 varied types of constructors, namely:

  • Default Constructor
  • Parameterized Constructor

You can also mix these two constructors for a single class called as…

  • constructor overloading

Default Constructor:

A no-argument constructor is referred to as a default constructor. Such constructors are defined to assign default values to the variable used by the class such as null, 0, 0.0 etc with respect to their data type.

A simple example of default constructor is as follows:

public class Radio
{
    Radio()
    {             
        System.out.println(“A constructor of Radio is invoked”);
    }

    public static void main(String args [])
    {
        Radio r = new Radio();
    }
}

Save and execute your Java program.

Please refer – compiling and running program in Java, if you are new to Java programming.

Output:

A constructor of Radio is invoked.

Here in the above-given example, a class Radio is created with a default constructor that gets called over the creation of the object.

Parameterized Constructor:

A constructor with a set of definite arguments is referred to as a parameterized constructor. Such constructors are used to offering distinct values to varied objects of any particular class.

A simple example of the parameterized constructor is:

public class School
{
    String name;
    String contact;

    School(String n, String c)
    {
        name = n;
        contact = c;
    }

    void printData()
    {
        System.out.println(“Name:”+name+”\tContact:”+contact);
    }

    public static void main(String args[])
    {
       School s1 = new School(“Oxford High School”,”9638527410”);

       School s2 = new School(“Cambridge International School”,”9175346097”);

       School s3 = new School(“Nath Valley Central School”,”8529637410”);

       s1.printData();

       s2.printData();

       s3.printData();
    }
}

Output:

Oxford High School         9638527410
Cambridge International School                                9175346097
Nath Valley Central School           8529637410

What is Constructor Overloading in Java?

Can we write multiple constructors in Java?

Yes, you can. [This question was asked in many job coding and interviews rounds.]

You can use multiple constructors in single calss. The types and number of parameters should not be the same for any of the constructor. When you create a class object, constructor gets called with matching (same types and a number of paratmenrts) constructor function.

It is also called Constructor Overloading in Java programming.

Basic Rules for writing Constructor in Java:

  • It is not necessary to write a constructor in your program.
  • By default, the compiler creates constructor at runtime.
  • The name of the constructor should be the same as the name of the class.
  • You can write multiple constructors for a single class object in Java.

So you understood the main difference between types of constructors in Java. If you have any point to discuss, kindly write a comment section below.


« Classes and Objects in JavaInheritance in Java »

JavaJava Oops Concepts
Pranati Paidipati
I am a graduate in computer science with a creative bent of mind for writing content. With great gusto, I enjoy learning new things. I trail in database management system, and object-oriented programming languages like Java, C/C++.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

Basic Java Tutorial

  1. Java- Tutorial Overview
  2. Java- Features & Characteristics
  3. Java-  Installation & Setup
  4. Java- Hello, World Program!
  5. Java- JDK vs JVM vs JRE
  6. Java- Data Types & Variables
  7. Java- String & its Methods
  8. Java- Different Operators Types
  9. Java- Flow Control Statements
  10. Java- Array
  11. Java- Exception Handling
  12. Java- ‘throw’ vs ‘throws’ Keyword
  13. Java- RegEx
  14. Java 12- New Advanced Features

Java OOPs concepts

  1. Java- OOPs Introduction
  2. Java- Classes & Objects
  3. Java- Constructor & Overloading
  4. Java- Method Overload vs Override
  5. Java- Access Modifiers
  6. Java- Abstraction
  7. Java- Inheritance
  8. Java- Interfaces
  9. Java- Nested Inner Classes

Java Advanced

  1. Java- Applet vs Application
  2. Java- HashMap Collections
  3. Java- ArrayList
  4. Java- LinkedList
  5. Java- Reverse Linked List
  6. Java- HashSet
  7. Java- HashMap vs HashSet
  8. Java-LinkedHashSet 

Java Exercise

50+ Java Coding Questions [Practice]

Java Projects

Patient Billing Software

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard