• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar

JavaTutorOnline

Java Tutor Online

  • Home
  • Courses
  • Tutorials
    • Java
    • Servlets
    • Struts
    • Spring
    • Webservice
  • FAQ
  • Testimonials
  • Blog
  • Contact Us

Constructor in Java and Overloaded Constructor Example Program

Constructor in Java – Explained with Examples

A constructor in Java is a special method used to initialize objects. It shares the same name as the class and has no return type. This tutorial explains the basics of constructors and covers constructor overloading in Java with real examples.

Watch: Java Constructor & Constructor Overloading (YouTube)

What is a Constructor?

  • A constructor is automatically called when an object is created.
  • It has the same name as the class.
  • It does not have a return type (not even void).

Example: Product Class

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Product {
    int id;
    String title;
    double price;
 
    // Default Constructor
    Product() {
        System.out.println("Default constructor called");
    }
 
    // Parameterized Constructor
    Product(int id, String title, double price) {
        this.id = id;
        this.title = title;
        this.price = price;
    }
}
 

In the above example this.id refers to the instance variable while only id refers to the parameter in the constructor

Constructor Overloading

Constructor overloading means creating multiple constructors in the same class with different parameter lists. This allows you to create objects in different ways.

Constructor vs Method in Java

ConstructorMethod
Same name as classCan have any name
No return typeMust have a return type
Called automaticallyCalled explicitly
Used to initialize objectUsed to perform actions

Summary

  • Java supports constructor overloading via different parameter types/count.
  • No return type — not even void.
  • Used to initialize objects with or without parameters.

Interview Tip:

Overloading is always based on parameters, not return type.

  • Java does not allow two constructors with the same parameter list even if the return type is different.
  • Be prepared to explain the flow of constructor invocation.

Need more help understanding this? Connect with online Java tutor for private tutoring or training.

Filed Under: Java Tagged With: constructor in java, constructor overloading in java, constructor vs method in java, java constructor example, java constructor tutorial, java oops concepts, java programming for beginners

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Mr Chinmay

Chinmay Patel
Online Java Tutor-Demo Class

Phone & Whatsapp +919853166385
[email protected]
Skype id: p.chinmay

Recent Posts

  • Constructor in Java and Overloaded Constructor Example Program
  • Important Interview Questions on Java Multithreading
  • React Spring Boot Web Services Integration
  • Spring Boot RESTful Web Services Example
  • Top Spring MVC Interview Questions and Answers for Developers
  • Top Spring Core Interview Questions and Answers for Developers
  • Host Java Web Apps for Free on Mobile with Tomcat and Termux
  • How to Deploy Java Web Application on Aws EC2 with Elastic IP
  • How to Learn Java in One day? | JavaTutorOnline
  • Simple Jsp Servlet Jdbc User Registration using Tomcat Mysql and Eclipse
Copyright © 2025 JavaTutorOnline